-2

Please have a look at my JavaScript

<script>
            function dynamicMessageLoader(id) {

                $.get("SentMessagesDynamicLoaderServlet?idMessageGroup=" + id, function (responseJson) {

                    $('#li_list').empty();
                    var array = [];

                    $.each(responseJson, function (index, item)

                    {
                        array[index] = item;
                    });

                    $('#message_count').empty();

                    var readArray = array.length;
                    var count = readArray + " Messages Selected";
                    $('<p />', {html: count}).appendTo('#message_count');

                    for (var i = 0; i < readArray; i++) {

                        //$('<li />', {html: array[i][1]}).appendTo('#li_list');
                        $('<li>', { 'class': 'clickable-row hand', id: array[i][0], html: array[i][1]}).appendTo('#li_list');

                    }
                });
            }
        </script>

Now the associated HTML

<ul id="li_list">
<li class='clickable-row hand' id="12" >text</li>                                  
</ul>

What happens is that when the above JavaScript code gets called, it adds <li> elements to li_list. As you can see, I have already hard coded one <li> element there as well.

Now when the <li> element is called, the below Jquery code needs to be executed.

<script>

            jQuery(function () {
                $('.clickable-row').click(function () {


                    var $this = $(this),
                            id = this.id;

                    //alert(id);

                    $.get("MessagesDynamicPopupServlet?idMessage=" + id, function (responseJson) {

                        var array = [];

                        $.each(responseJson, function (index, item)

                        {
                            array[index] = item;
                        });
                        var image = array[3];

                        $('#idMessage').val(array[0]);
                        $('#idMessageGroup').val(array[1]);
                        $('#message').val(array[2]);

                        $('#blah').attr("src", image);

                        $('#videoURL').val(array[4]);
                    });
                });
            });

        </script>   

Now the thing is, the above Jquery code gets called only for the hard coded <li>. It never gets called for the <li> element I added via the JavaScript.

How can I fix this?

vijay
  • 493
  • 5
  • 19
PeakGen
  • 21,894
  • 86
  • 261
  • 463

1 Answers1

1

Just use the Delegated events , for the newly created elements . so here you'll attach an event to a parent (document in this case ) , that will be fired for all it's descendants ( second params in the .on function ) which will be the .clickable-row class in your case .

It wont work for $('.clickable-row').click because this last is attached to only existing dom node at the moment of the event assignment ( so newly created wont be attached to this click ).

See below code :

$(document).on("click", '.clickable-row', function() {


  var $this = $(this),
    id = this.id;

  //alert(id);

  $.get("MessagesDynamicPopupServlet?idMessage=" + id, function(responseJson) {

    var array = [];

    $.each(responseJson, function(index, item)

      {
        array[index] = item;
      });
    var image = array[3];

    $('#idMessage').val(array[0]);
    $('#idMessageGroup').val(array[1]);
    $('#message').val(array[2]);

    $('#blah').attr("src", image);

    $('#videoURL').val(array[4]);
  });
});
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52
  • 1
    if you dont mind would you explain why this works ? – Rajshekar Reddy Jun 27 '17 at 07:07
  • Okey i'll do now – Bourbia Brahim Jun 27 '17 at 07:08
  • wow great. Yes, please explain why this works too. – PeakGen Jun 27 '17 at 07:09
  • @PeakGen added in answer :) – Bourbia Brahim Jun 27 '17 at 07:13
  • 1
    When you call `$('.clickable-row')` it will search all such elements and attach events (like 'click'). It will do so only for existing objects. When you use `$(document).on("click", '.clickable-row', function(){});` It will attach event on `document` (which always exists), and when you click on `.clickable-row` this click event will bubble down to document where script checks which element was clicked and executes attached event. – Volvox Jun 27 '17 at 07:41