javascript - JQuery- Change the id of an element and call a function on click -


i have element id, changed id , when click on element (with new id now), still calls function previous id

$('#1st').click(function(){      $('#1st').attr('id','2nd');      alert("id changed 2nd");  });  $('#2nd').click(function(){      alert("clicked on second");  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <a href="javascript:;" id="1st">click</a>

example here

because add event before element exists. not find element. either attach event when change id or need use event delegation.

    $('#1st').on("click.one", function(e) {        $('#1st').attr('id', '2nd');        alert("id changed 2nd");        e.stoppropagation();        $(this).off("click.one");      });      $(document).on("click", '#2nd', function() {        alert("clicked on second");      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <a href="#" id="1st">click</a>


Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -