1

I have a problem with event triggering in my codeigniter project. I want to load a table dynamically in my page. This table is returned from my controller through an ajax call. And I want to trigger a click event in a span in that table. I tried this in many ways, but its not working. I am attaching my code here. please help me to find a solution.

controller :

$str = '<table>
         <tr>
            <td class="cost date" id="date_'.$request['SupplyID'].'">
               <span class="editable" style="display: none;">
                  <input type="text" class="expiry" autocomplete="off" data-date-id="date_'.$request['SMSSupplyID'].'">                                    
               </span>
               <span class="cnt" style="display: inline;">Choose</span>
            </td>
          </tr>
       </table>';
echo $str;

script in view :

function loadmore()
    {
        var url;

        url =  '/SMS/SMS/loadMoreRequests';

        $.ajax({
            type: 'post',
            url: url,
            data: {
                limit:val
            },
            success: function (response) { console.log(response);

                $("#requestListTable").html(response); 
                val +=10;
            }
        });
    }

$('.date').find('.cnt').on('click', function(){ 
    console.log(1);       // for testing
});

I tried the following changes, but no use

1)

$('.date').find('.cnt').live('click', function(){ 
    console.log(1);       // for testing
});

2)

$('.date').find('.cnt').click(function(){ 
    console.log(1);       // for testing
});
1
  • Try my answer I hope it will solve your issue. Commented Oct 18, 2017 at 5:13

3 Answers 3

2
$("container for the table").on("click", ".cnt", function() {


});

You can use the event delegation for this. Bind the event to a parent element which is present on the dom on document ready. Or you can simply use,

$("body").on("click", ".cnt", function() {


});
Sign up to request clarification or add additional context in comments.

Comments

2

You can try this

$(document).find(".cnt").on("click",function(){
    console.log(1);       // for testing
});

1 Comment

using just 'on' doesn't mean 'event delegation'. Your code is just as a normal click event binding.
1

You need to delegate the Event to the dynamically created elements.

 $(document).on('click','.cnt',function(){
  alert("Processing comes here");
 });

1 Comment

@geeth Thanks for accepting my answer . Happy to help you.

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.