quersfas quersfas - 1 year ago 54
jQuery Question

About jQuery after

Suppose my HTML input looks like this :

<input type="text" class="hello">

I used jQuery and inserted element after it like this. AND it uses ajax

$(".hello").after("<p>"+response."</p>"); //response means things that come from ajax.

But after creates new element
on every response...

How do I insert element jQuery class
so it get updated according to ajax instead of inserting

I don't have any idea how to do it . :(


Answer Source


//find the next element of .hello
var $p = $(".hello").next();
//if it is not a p element
if (!$p.is('p')) {
    //create a new p element and insert it after .hello
    $p = $('<p />').insertAfter('.hello');
//update its text