0

I have this snippet in jQuery. I append an input text when a user clicks on an add sign that has an id 'clickEventOnPlus'. Within this appened code, there is a minus sign (defined in within ) that has another class clickEventOnMinus.

$('#clickEventOnPlus').click(function(){
   var addDestination2 = '<input type="text" <span><i style="font-size:18px" class="fa clickEventOnMinus">&#xf068;</i></span> <br> <br>'
   $(".addDestination").append(addDestination2);
});

How do I add an event listener on clickEventOnMinus? Because when I try the following code, it doesn't work. Thanks!

$('.clickEventOnMinus').click(function(){
     alert("W");
});
Laura
  • 268
  • 2
  • 9
abidishajia
  • 222
  • 1
  • 6
  • 15

4 Answers4

2

we can use delegated events to bind the click event to dynamically created elements and also to avoid the need to frequently attach and remove event handlers.

$(document).on('click', '.clickEventOnMinus', function() {
 alert("W");
});
rrk
  • 15,677
  • 4
  • 29
  • 45
4b0
  • 21,981
  • 30
  • 95
  • 142
1

You need to put the .click after the element appended, otherwise, the selector cannot found the element

<script>
    $(document).ready(function(){
        $('#clickEventOnPlus').click(function(){
           var addDestination2 = '<input type="text" <span><i style="font-size:18px" class="fa clickEventOnMinus">&#xf068;</i></span> <br> <br>'
           $(".addDestination").append(addDestination2);

            $('.clickEventOnMinus').click(function(){
                 alert("W");
            });
        });
     });
</script>
Krishna Soni
  • 95
  • 1
  • 9
Stanley Cheung
  • 929
  • 7
  • 22
0

Try this

$(".addDestination").on('click', '.clickEventOnMinus', function(){
     alert("W");
});
Sagar Jajoriya
  • 2,377
  • 1
  • 9
  • 17
0

i've made a demo , be aware with the order of your code as the element is added to the DOM the event is added too

<div id="clickEventOnPlus">
hola
</div>
<div class="addDestination">

</div>
$('#clickEventOnPlus').click(function(){
   var addDestination2 = $('<input type="text" <span><i style="font-size:18px" class="fa clickEventOnMinus">&#xf068;</i></span> <br> <br>');
   $(".addDestination").append(addDestination2);
   console.log(addDestination2);
   $('.clickEventOnMinus').click(function(){
     alert("W");
});
});
Álvaro Touzón
  • 1,247
  • 1
  • 8
  • 21
  • Thanks! Another quick question - How would I delete a specific appened item? If I want to use the minus sign to delete that specific input text, how would I go about that? – abidishajia Oct 26 '17 at 06:21
  • @abidishajia with remove() https://api.jquery.com/remove/ – Álvaro Touzón Oct 26 '17 at 06:25
  • I added this, but it deletes all the appended input texts fields. I want it to delete a specific one. `$('#clickEventOnPlus').click(function(){ var addDestination2 = ' 

    ' $(".addDestination").append(addDestination2); $('.clickEventOnMinus').click(function(){ $('.removeDest').remove(); }); }); ` @Álvaro Touzón
    – abidishajia Oct 26 '17 at 19:27