0

Hi I am trying to to replace already created list by new list dynamically using jquery. But on newly created list, I am not able to use any jquery event.

I have created a list of players

  • Sachin
  • Sourav
  • Rahul

On clicking any list name it is alerting its id, then a newly created list is replacing dynamically using jquery.

  • Kohli
  • MS
  • Rahane

But I am not able to pick the click event for newly created list. It is not showing any error in console and nothing happening when we click any name of new dynamic list.

<html>
<head>

<script src="jquery-3.1.1.js"></script>
<script>
$(document).ready(function(){
    $("#videoul li").click(function(){
         var vocab_id = $(this).attr('id');
         alert("Hi the ID is " + vocab_id);

         var aaila = '<a href="#" id="i_am_link">Click me again</a><ul id = "videoul"><li id="Kohli">Virat</li><li id="Dhoni">M S</li><li id="Rahane">Ajinkya</li></ul>';

         $('#videowrapper').html(aaila);
    });
});
</script>
</head>
<body>

<div id="videowrapper"><a href="#abc" id="i_am_link">Click me</a>
    <ul id="videoul">
        <li id="Tendulkar">Sachin</li>
        <li id="Ganguly">Sourav</li>
        <li id="Dravid">Rahul</li>
    </ul>
</div>


</body>
</html>

I am clueless now, how to make this working.

1
  • 1
    You could use delegated events, but in your case it seems quite obvious that you could just replace the content inside each LI when clicked, instead of replacing the entire list Commented Jan 16, 2017 at 19:19

2 Answers 2

1

Use "on" method. Instead of $("body") selector you can use other element that is not dynamically created.

$('body').on('click', '#videoul li', function() {
    var vocab_id = $(this).attr('id');
    alert("Hi the ID is " + vocab_id);

    var aaila = '<a href="#" id="i_am_link">Click me again</a><ul id = "videoul"><li id="Kohli">Virat</li><li id="Dhoni">M S</li><li id="Rahane">Ajinkya</li></ul>';

    $('#videowrapper').html(aaila);
});
Sign up to request clarification or add additional context in comments.

2 Comments

It is preferable to add the listener to the nearest non-generate element. In this case, I'd suggest you use $('#videowrapper') instead of $('body') .
Yes you are right. I just wasn't sure which part of his code is 'non-generated'.
0

Since you mentioned dynamically created elements, you need to use event delegation with on

Check out this snippet

$("body").on("click", "#videoul li", function(){
    var vocab_id = $(this).parent().attr('id');
    console.log("Hi the ID is " + vocab_id);
    
    var aaila = '<a href="#" id="i_am_link">Click me again</a><ul id = "videoul"><li id="Kohli">Virat</li><li id="Dhoni">M S</li><li id="Rahane">Ajinkya</li></ul>';
    
    $('#videowrapper').html(aaila);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="videowrapper"><a href="#abc" id="i_am_link">Click me</a>
    <ul id="videoul">
        <li id="Tendulkar">Sachin</li>
        <li id="Ganguly">Sourav</li>
        <li id="Dravid">Rahul</li>
    </ul>
</div>

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.