1

With jquery on several event i required to add elements but on that elements click event is not triggered. I tried to add the listener in the ready function also but it doesn't worked. Anyone please help.

 $(document).ready(function(){
   $('#button').click(function(){
       var toAdd = $('input[name=checkListItem]').val();
       $('div.list').append('<div class="item-next">' + toAdd + '</div>')

   });
 
});

 $('.item-next').click(function(){
      alert()

   });
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!DOCTYPE html>
<html>
    <head>
     <title>Find Listeners</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
        <script type="text/javascript" src="script.js"></script>
 </head>
 <body>
  <h2>Find Listeners</h2>
  <form name="checkListForm">
   <input type="text" name="checkListItem"/>
  </form>
  <button id="button">Click to Add</button>
  <br/><br/><br/>
      List
  <div class="list"></div>
 </body>
</html>

5 Answers5

1

Append event element Not work with normal click .so try with on()kindly see the important of that

 $(document).on()

It helpfull to learn on() Why use jQuery on() instead of click()

$(document).ready(function(){
   $('#button').click(function(){
       var toAdd = $('input[name=checkListItem]').val();
       $('div.list').append('<div class="item-next">' + toAdd + '</div>')

   });
 
});

 $(document).on('click','.item-next',function(){
      alert('its work')

   });
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!DOCTYPE html>
<html>
    <head>
     <title>Find Listeners</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
        <script type="text/javascript" src="script.js"></script>
 </head>
 <body>
  <h2>Find Listeners</h2>
  <form name="checkListForm">
   <input type="text" name="checkListItem"/>
  </form>
  <button id="button">Click to Add</button>
  <br/><br/><br/>
      List
  <div class="list"></div>
 </body>
</html>
Community
  • 1
  • 1
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

Try using quotation on 'checkListItem'

var toAdd = $("input[name='checkListItem']").val();
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
0

well when you dynamically create an element use following structure as event listener:

$(document).on('click','.item-next',function(){
  alert();
});
Sam
  • 1,424
  • 13
  • 28
0

Try this (resolved). learn more about delegate methods

 $(document).ready(function(){
 $('#button').click(function(){
 var toAdd = $('input[name=checkListItem]').val();
 $('div.list').append('<div class="item-next">' + toAdd + '</div>')

 });
 $('div.list').on('click', '.item-next', function(){
  $(this).html($(this).html()+"<span style='color:red;'> clicked")
  alert('clicked on '+$(this).html());
   });
   });

 
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!DOCTYPE html>
<html>
    <head>
     <title>Find Listeners</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
        <script type="text/javascript" src="script.js"></script>
 </head>
 <body>
  <h2>Find Listeners</h2>
  <form name="checkListForm">
   <input type="text" name="checkListItem"/>
  </form>
  <button id="button">Click to Add</button>
  <br/><br/><br/>
      List
  <div class="list"></div>
 </body>
</html>
Smit
  • 395
  • 3
  • 12
0

This gonna work for you

 $(document).ready(function(){
   $('#button').click(function(){
       var toAdd = $('input[name=checkListItem]').val();
       $('div.list').append('<div class="item-next">' + toAdd + '</div>')

   });
 $(document).on("click",".item-next",function(){alert("myalert")})
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!DOCTYPE html>
<html>
    <head>
     <title>Find Listeners</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
        <script type="text/javascript" src="script.js"></script>
 </head>
 <body>
  <h2>Find Listeners</h2>
  <form name="checkListForm">
   <input type="text" name="checkListItem"/>
  </form>
  <button id="button">Click to Add</button>
  <br/><br/><br/>
      List
  <div class="list"></div>
 </body>
</html>
satan code
  • 111
  • 2