I am trying to make a simple functionality where user selects some value from drop down and then clicks on the add button to add the selected item in the below div in the form of tag.
Each added tag has a remove anchor which when clicked removes the tag.
Now when clicked on add button the tags are being inserted correctly, But when I clicked on the remove button over tag, the click event is not firing.
However if I hard coded some tags with exact same markup as that of dynamically generated tags, the click event for remove tag is firing exact properly and the tag is being removed as I wanted.
HTML:
<select id="ddlTagName">
<option value="1">Tag One</option>
<option value="2">Tag Two</option>
<option value="3">Tag Three</option>
</select>
<input id="btnInsertTag" type="button" value="Add"/>
<br/>
<div id="TagsHolder">
<span class="tag">
<span>Tag One HardCoded </span>
<a class="remove">X</a>
</span>
<span class="tag">
<span>Tag Two HardCoded </span>
<a class="remove">X</a>
</span>
</div>
JS:
$("#btnInsertTag").click(function () {
var selectedTagText = $("#ddlTagName option:selected").text();
var selectedTagValue = $('#ddlTagName').val();
var generateMarkup = '<span class="tag" data-value="' + selectedTagValue + '"><span>' + selectedTagText + ' </span><a class="remove">X</a></span>';
$("#TagsHolder").append(generateMarkup);
});
$(".remove").click(function () {
alert('click event triggered');
$(this).parent(".tag").remove();
});
My question is that why the click event is not firing for the dynamically generated tags.
Here is the Demo
Thanks in advance
$(document). ready(function() { $(document). on("click","button . delete-row",function(){ // Do something when button is clicked }); });
Syntax of jQuery “.on() method to add an event (click in our case) to the <span>, which is a descendant (child) of the <div> element. Any <span> element that we add to the <div> now has a click event attached to it. In the above example, I have used the span keyword as childSelector to the . on() method.
You can use the live() method to bind elements (even newly created ones) to events and handlers, like the onclick event. Save this answer.
Use even delegation instead
Event delegation allows us to attach a single event listener, to a parent element, that will fire for all children matching a selector, whether those children exist now or are added in the future.
More info
$(document).on('click', '.remove', function () {.....
$("#btnInsertTag").on('click', function () {
var selectedTagText = $("#ddlTagName option:selected").text();
var selectedTagValue = $('#ddlTagName').val();
var generateMarkup = '<span class="tag" data-value="' + selectedTagValue + '"><span>' + selectedTagText + ' </span><a class="remove">X</a></span>';
$("#TagsHolder").append(generateMarkup);
});
$(document).on('click', ".remove", function () {
alert('click event triggered');
$(this).parent(".tag").remove();
});
event binding will not work for dynamically generated elements. For this you need to bind to an element that exists at the moment that the JS runs (which is the document), and supply a selector in the second parameter to .on(). When a click occurs on document element jQuery checks whether it applied to a child of that element which matches the ".remove" selector.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With