user1814174 user1814174 - 3 months ago 19
Ajax Question

jQuery selector for ajax loaded content

I have used this library:
http://www.jacklmoore.com/autosize/

and in order to use it I should use syntax like below:

autosize($('textarea#description'));


or any other javascript selector.
But my textarea loaded by ajax and it is not working.

Is there any other selector that works with ajax elements?
Thank you

Answer

Just wrap the jquery selector around it and use .find() to narrow it down.

$.ajax({...}).done(function(responseHTML){
    // depending on what the function is doing you may need to append it to the body first.
    $(responseHTML).appendTo('body'); // or wherever you want to put it
    $textarea = $(responseHTML).find('#description');
    autosize($textarea);
});
Comments