Gold Pearl Gold Pearl - 4 months ago 17
jQuery Question

How to add div dynamically inside a div by class - Jquery

I want dynamically add info div into every dynamic class.

HTML

<div id='container'>
<div class='dynamic'></div>
<div class='dynamic'></div>
<div class='dynamic'></div>
<div class='dynamic'></div>
</div>


I want like this.

<div id='container'>
<div class='dynamic'><div class="info">info</div></div>
<div class='dynamic'><div class="info">info</div></div>
<div class='dynamic'><div class="info">info</div></div>
<div class='dynamic'><div class="info">info</div></div>
</div>


JS FIDDLE

Answer

To add an info div into every class using jQuery, simply use:

$( ".dynamic" ).append( "<div class='info'>info</div>" );

The JSFiddle demonstrates it.

If you want to continually check for .dynamic classes, you can use something like this:

$(document).ready(setInterval(function(){
    $( ".dynamic" ).append( "<div class='info'>info</div>" );
}, 1000));

In the above case, you are checking for .dynamic classes every 1000ms (1 second).

Hope this helps.