Ravichandran Jothi
Ravichandran Jothi

Reputation: 3066

How to add a div when clicking a button in jquery?

I need to add the following div structure when i click button at every time? I need to generate the following whole div structure at every time of clicking the button using jquery.

<div class="module_holder">
<div class="module_item">
<img src="images/i-5.png" alt="Sweep Stakes"><br>sendSMS
</div>
</div>

help me.

Thanks Ravi

Upvotes: 10

Views: 75870

Answers (3)

James
James

Reputation: 5169

Try this out http://jsfiddle.net/4dEsJ/1/ I'm sure there are better ways of creating the element, but if the structure and content remains the same, this works!

Upvotes: 3

BenM
BenM

Reputation: 53198

How about this:

$('#button_id').click(function() {

    var structure = $('<div class="module_holder"><div class="module_item"><img src="images/i-5.png" alt="Sweep Stakes" /><br />sendSMS</div></div>');
   $('#whatever').append(structure); 

});

Upvotes: 1

rtpHarry
rtpHarry

Reputation: 13125

You need a container div that you can insert it into:

<div id="container"></div>

Then you can use the append() method of jquery:

$("#somebutton").click(function () {
  $("#container").append('<div class="module_holder"><div class="module_item"><img src="images/i-5.png" alt="Sweep Stakes"><br>sendSMS</div></div>');
});

Upvotes: 24

Related Questions