Interacting with ajax response items

I'm using codeIgniter for building a web application.

I currently have a controller that loads an index action, and an index view.

Inside this view, i use ajax to call another controller action, and fetch items from a database, i load them into a div.

What would be the approach to being able to interact with these objects, by, let's say Jquery on click events.

$("#other").click(function() {
 $("#target").click();
});

The above obviously doesn't work.

I can't figure out how to do this with ajax calls, because they load after the dom is ready

Upvotes: 1

Views: 69

Answers (2)

GautamD31
GautamD31

Reputation: 28763

Try with .trigger like

$(document).ready(function(){
    $("#other").click(function() {
        $("#target").trigger('click');
    });
})

Upvotes: 0

bipen
bipen

Reputation: 36531

you need to delegate the dynamically generated elements to there closest static parent using on...

here i am using document as parent.. but it is recommended to use the closest static parent which is present when element is appended.. and `trigger()`` to trigger the click

$(document).on('click',"#other",function() {
    $("#target").trigger('click');
});

Upvotes: 1

Related Questions