quersfas quersfas - 5 months ago 6
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
"<p>"+response."</p>"
on every response...

How do I insert element jQuery class
.hello
so it get updated according to ajax instead of inserting
"<p>"+response."</p>"..


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

Regards

Answer

Try

//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
$p.text(response)
Comments