-1

I am trying to figure out how to fix an error the Chrome console revealed for the theme I use that has been there for quite some time. I don't know javascript so not sure how to fix it, after some research and reading my only guess is .live should be .on as per the error it is using JQuery 1.12.4 and I read .live was removed starting in version 1.9. I posted on the theme makers forum, but I figure I would get help quicker here. Here is the code block the error points to, it points to ".live('click', function(event){" but includes the whole section, I did notice a missing } at the end as well, I left the block below exactly how it is in the functions.js file.

$('.hm_icon_search > a, .top_add_card').live('click', function(event){ var parent = $(this).parent(); var $this_btn = $(this); var $target_block = $this_btn.siblings('div'); event.preventDefault(); event.stopPropagation(); if(parent.hasClass('active') && $target_block.hasClass('hm_active_prep')){ $target_block.fadeOut(function(){ parent.removeClass('active'); } } }); 
1
  • Hi, welcome to WordPress Development. As a rule, many in the community consider questions about third-party themes and plugins to be off-topic so you might notice some down-votes and answers may be slow if they come at all. You are likely to be pointed to the theme author for support. You can find out what questions are a good fit here.CommentedAug 16, 2020 at 15:23

1 Answer 1

1

If you asking what is the replacement for .live() it's .on()`, for enabling event handling for dynamically added elements and that's how you would use it.

$(document).on("click","#test-element",function() {}); 

so for your code.

$('body').on('click', '.top_add_card', function(event){ var parent = $(this).parent(); var $this_btn = $(this); var $target_block = $this_btn.siblings('div'); event.preventDefault(); event.stopPropagation(); if(parent.hasClass('active') && $target_block.hasClass('hm_active_prep')){ $target_block.fadeOut(function(){ parent.removeClass('active'); } } }); 

This would work for dynamically adding elements too. because thats the main purpose of using .on(). read more about it here. https://api.jquery.com/on/

4
  • I will give it a try, thanks for getting your reply in before they closed the question, I have not heard from the theme maker yet, I found the JQuery page for .live which showed what happened in 1.9 when .on replaced it and I tried it, but dont know JS so probably did it wrong, I found it interesting as in a section way above it, it has a .live section as well, but Chrome only shows an error for the section I postedCommentedAug 18, 2020 at 2:01
  • It worked! Thanks so much, marked your answer as correct, I looked through the code and it looks like they switched to on, but forgot 2 sections, could you help with this one as well? Here is the main section with .live "$(this).live('click',function () {" using your code would it be this "$(this).on('click',function () {" or is this one OK?CommentedAug 18, 2020 at 2:11
  • $('body').on('click', this, function () {CommentedAug 19, 2020 at 2:47
  • Thank you so very much, I extremely appreciate it!CommentedAug 20, 2020 at 1:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.