borayeris borayeris - 1 year ago 97
jQuery Question

jQuery: combining after and html methods

As you know in jQuery we can combine methods. But here I have a problem.



$('div').after('<span class="text-warning" >SPAN</span>').html('test');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>DIV</div>





.html inserts the text in div. Shouldn't it change new element?

Answer Source

No, 'after' is an operator and will return whatever it operated on. In this case, the div.

What you want is something like:

var span = $('<span class="text-warning" >SPAN</span>')
$('div').after(span)
span.html('test');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download