jQuery how to bind onclick event to dynamically added HTML element

JavascriptJqueryBind

Javascript Problem Overview


I want to bind an onclick event to an element I insert dynamically with jQuery

But It never runs the binded function. I'd be happy if you can point out why this example is not working and how I can get it to run properly:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da" lang="da">
        <head>
          <title>test of click binding</title>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
          <script type="text/javascript">


        jQuery(function(){
          close_link = $('<a class="" href="#">Click here to see an alert</a>');
          close_link.bind("click", function(){
            alert('hello from binded function call');
            //do stuff here...
          });
  
          $('.add_to_this').append(close_link);
        });
          </script>
        </head>
        <body>
          <h1 >Test of click binding</h1>
          <p>problem: to bind a click event to an element I append via JQuery.</p>

          <div class="add_to_this">
            <p>The link is created, then added here below:</p>
          </div>

          <div class="add_to_this">
            <p>Another is added here below:</p>
          </div>


        </body>
        </html>

EDIT: I edited the example to contain two elements the method is inserted to. In that case, the alert() call is never executed. (thanks to @Daff for pointing that out in a comment)

Javascript Solutions


Solution 1 - Javascript

All of these methods are deprecated. You should use the on method to solve your problem.

If you want to target a dynamically added element you'll have to use

$(document).on('click', selector-to-your-element , function() {
     //code here ....
});

this replace the deprecated .live() method.

Solution 2 - Javascript

The first problem is that when you call append on a jQuery set with more than one element, a clone of the element to append is created for each and thus the attached event observer is lost.

An alternative way to do it would be to create the link for each element:

function handler() { alert('hello'); }
$('.add_to_this').append(function() {
  return $('<a>Click here</a>').click(handler);
})

Another potential problem might be that the event observer is attached before the element has been added to the DOM. I'm not sure if this has anything to say, but I think the behavior might be considered undetermined. A more solid approach would probably be:

function handler() { alert('hello'); }
$('.add_to_this').each(function() {
  var link = $('<a>Click here</a>');
  $(this).append(link);
  link.click(handler);
});

Solution 3 - Javascript

How about the Live method?

$('.add_to_this a').live('click', function() {
    alert('hello from binded function call');
});

Still, what you did about looks like it should work. There's [another post][1] that looks pretty similar.

[1]: https://stackoverflow.com/questions/584605/appending-html-w-click-event-using-jquery "another posting"

Solution 4 - Javascript

A little late to the party but I thought I would try to clear up some common misconceptions in jQuery event handlers. As of jQuery 1.7, .on() should be used instead of the deprecated .live(), to delegate event handlers to elements that are dynamically created at any point after the event handler is assigned.

That said, it is not a simple of switching live for on because the syntax is slightly different:

New method (example 1):

$(document).on('click', '#someting', function(){
	
});

Deprecated method (example 2):

$('#something').live(function(){

});

As shown above, there is a difference. The twist is .on() can actually be called similar to .live(), by passing the selector to the jQuery function itself:

Example 3:

$('#something').on('click', function(){

});

However, without using $(document) as in example 1, example 3 will not work for dynamically created elements. The example 3 is absolutely fine if you don't need the dynamic delegation.

> Should $(document).on() be used for everything?

It will work but if you don't need the dynamic delegation, it would be more appropriate to use example 3 because example 1 requires slightly more work from the browser. There won't be any real impact on performance but it makes sense to use the most appropriate method for your use.

> Should .on() be used instead of .click() if no dynamic delegation is needed?

Not necessarily. The following is just a shortcut for example 3:

$('#something').click(function(){

});

The above is perfectly valid and so it's really a matter of personal preference as to which method is used when no dynamic delegation is required.

References:

Solution 5 - Javascript

Consider this:

jQuery(function(){
  var close_link = $('<a class="" href="#">Click here to see an alert</a>');
	  $('.add_to_this').append(close_link);
	  $('.add_to_this').children().each(function()
	  {
		$(this).click(function() {
			alert('hello from binded function call');
			//do stuff here...
		});
	  });
});

It will work because you attach it to every specific element. This is why you need - after adding your link to the DOM - to find a way to explicitly select your added element as a JQuery element in the DOM and bind the click event to it.

The best way will probably be - as suggested - to bind it to a specific class via the live method.

Solution 6 - Javascript

It is possible and sometimes necessary to create the click event along with the element. This is for example when selector based binding is not an option. The key part is to avoid the problem that Tobias was talking about by using .replaceWith() on a single element. Note that this is just a proof of concept.

<script>
	// This simulates the object to handle
	var staticObj = [
		{ ID: '1', Name: 'Foo' },
		{ ID: '2', Name: 'Foo' },
		{ ID: '3', Name: 'Foo' }
	];
	staticObj[1].children = [
		{ ID: 'a', Name: 'Bar' },
		{ ID: 'b', Name: 'Bar' },
		{ ID: 'c', Name: 'Bar' }
	];
	staticObj[1].children[1].children = [
		{ ID: 'x', Name: 'Baz' },
		{ ID: 'y', Name: 'Baz' }
	];
	
	// This is the object-to-html-element function handler with recursion
	var handleItem = function( item ) {
		var ul, li = $("<li>" + item.ID + " " + item.Name + "</li>");

		if(typeof item.children !== 'undefined') {
			ul = $("<ul />");
			for (var i = 0; i < item.children.length; i++) {
				ul.append(handleItem(item.children[i]));
			}
			li.append(ul);
		}
		
		// This click handler actually does work
		li.click(function(e) {
			alert(item.Name);
			e.stopPropagation();
		});
		return li;
	};
	
	// Wait for the dom instead of an ajax call or whatever
	$(function() {
		var ul = $("<ul />");
		
		for (var i = 0; i < staticObj.length; i++) {
			ul.append(handleItem(staticObj[i]));
		}
		
		// Here; this works.
		$('#something').replaceWith(ul);
	});
</script>
<div id="something">Magical ponies ♥</div>

Solution 7 - Javascript

	function load_tpl(selected=""){
		$("#load_tpl").empty();
		for(x in ds_tpl){
			$("#load_tpl").append('<li><a id="'+ds_tpl[x]+'" href="#" >'+ds_tpl[x]+'</a></li>');
		}
		$.each($("#load_tpl a"),function(){
			$(this).on("click",function(e){
				alert(e.target.id);
			});
		});
	}

Solution 8 - Javascript

I believe the good way it to do:

$('#id').append('<a id="#subid" href="#">...</a>');
$('#subid').click( close_link );

Solution 9 - Javascript

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 	
<script>
	$(document).ready(function(){
		$(document).on('click', '.close', function(){
			var rowid='row'+this.id;
			var sl = '#tblData tr[id='+rowid+']';
			console.log(sl);
			$(sl).remove();
		});
		$("#addrow").click(function(){
			var row='';
			for(var i=0;i<10;i++){
				row=i;
				row='<tr id=row'+i+'>'
					+	'<td>'+i+'</td>'
					+	'<td>ID'+i+'</td>'
					+	'<td>NAME'+i+'</td>'
					+	'<td><input class=close type=button id='+i+' value=X></td>'
					+'</tr>';
				console.log(row);
				$('#tblData tr:last').after(row);
			}
		});
	});

</script>
</head>
  <body>
 	<br/><input type="button" id="addrow" value="Create Table"/>
 	<table id="tblData" border="1" width="40%">
 		<thead>
 		<tr>
 			<th>Sr</th>
 			<th>ID</th>
 			<th>Name</th>
 			<th>Delete</th>
 		</tr>
 		</thead>
	</table>
	</body>
 </html>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJesper R&#248;nn-JensenView Question on Stackoverflow
Solution 1 - JavascriptaM1NeView Answer on Stackoverflow
Solution 2 - JavascriptTobiasView Answer on Stackoverflow
Solution 3 - JavascriptBrother ErrynView Answer on Stackoverflow
Solution 4 - JavascriptMrCodeView Answer on Stackoverflow
Solution 5 - JavascriptDaffView Answer on Stackoverflow
Solution 6 - JavascriptJoel PeltonenView Answer on Stackoverflow
Solution 7 - JavascripttanthucView Answer on Stackoverflow
Solution 8 - JavascriptyogsotothView Answer on Stackoverflow
Solution 9 - JavascriptSudeepta MajheeView Answer on Stackoverflow