user38208 user38208 - 1 year ago 54
HTML Question

Use Jquery to add a div dynamically based on text

I am trying to add a div dynamically based on a the text of the parent div.

My html is:

<div class="listing_detail col-md-4"><strong>Living Room:</strong> Yes</div>
<div class="listing_detail col-md-4"><strong>Kitchen:</strong> No</div>

So I want a div to be insert such that if YES,
<div class="fa fa-check"></div>
should be added right before the
and if No
<div class="fa fa-cross"></div>
should be added.

Using the jQuery below i have managed to add a class to
<div class="listing_detail col-md-4">
however i cant figure out how to add the div.




Answer Source

Use .prepend():

$('.listing_detail:contains("Yes")').prepend('<div class="fa fa-check"></div>');

This would result in:

<div class="listing_detail col-md-4"><div class="fa fa-check"></div><strong>Living Room:</strong> Yes</div>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download