0

I have this function:

$(".action").click(function(){            
$.get('suppliers/template/get_options_list.php?action='+action+'&id='+id+'&optvalue='+optvalue+'&pid='+pid+'',
update_options);        
    }    
});    
function update_options(options){
$('#selectedoptions').html(options);    
}       

It works fine for the first click trigger but after ajax returns results next click doesnt fire.. No errors, console is clear but stilll

What could cause this? Thanks

12
  • If .action element is inside #selectedoptions element, it is either doesn't exist after replacing html or it is replaced with same one, and then it is time to use delegated event handler $('#selectedoptions').on('click', ".action", function() { Commented Dec 12, 2014 at 19:15
  • Does update_options update the HTML that contains .action elements? If so, you'll need to delegate your click event. Hah, yeah, what Regent said. Commented Dec 12, 2014 at 19:16
  • 2
    It looks like you have an extra } in your click function. Is this code even running? Commented Dec 12, 2014 at 19:16
  • Yes, the returned element .action is in #selectedoptions but why this is a reson not to fire? Commented Dec 12, 2014 at 19:17
  • @Europeuser question about binding event handlers to dynamically created elements is asked many times every day. Commented Dec 12, 2014 at 19:19

3 Answers 3

1
$(document).on('click','.action',function(){
$.get('suppliers/template/get_options_list.php?action='+action+'&id='+id+'&optvalue='+optvalue+'&pid='+pid+'',
update_options);      
});    
function update_options(options){
$('#selectedoptions').html(options);
}

and if .action if a submit input use

$(document).on('submit','your_form_ID_or_Class',function()
Sign up to request clarification or add additional context in comments.

4 Comments

A little explanation would be helpful. Also, "action" is a class and not an ID.
yes, mate, it is a class, group of elements that fire , and as I said the first trigger works fine and after the ajax returns the html which consists the same .action elements but they dont fire..
@showdev thanks :) I updating my answer while you post your comment .. but I'm very slow English typing :)
FYI.. you can bind multiple events using on event listener e.g: $(document).on('click submit','your_form_ID_or_Class',function()
1

Try binding the click event to the body, rather than the specific element, which won't be bound anymore once you replace the html:

    $("body").on("click", ".action", function(){            
      $.get('suppliers/template/get_options_list.php?action='+action+'&id='+id+'&optvalue='+optvalue+'&pid='+pid+'',
update_options);        
    }    
});    
function update_options(options){
$('#selectedoptions').html(options);    
}       

Comments

0

I removed the ajax returned html and instead of it on ajax response I used .remove() function to remove elements instead of generating new html.. Thank you for your help guys ! This isnt the original solution but it works fine..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.