Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Div Dynamically using JQuery

Tags:

html

jquery

I'm not sure if this would be the best option.. But I want the option that when the user clicks a button, it will add another div or li.

I am going to allow users to upload documents, but multiple ones. I'd like to for the user to be able to click a button, and a new <div> or <li> is generated with predefined code. Is this possible?

Here's a fiddle..

http://jsfiddle.net/AHvwP/1/

like image 611
JD Audi Avatar asked May 02 '11 01:05

JD Audi


2 Answers

Try this:

$('.button').click(function() {
    $('#myContainer').append('<div>the new guy</div>');
});
like image 179
theabraham Avatar answered Oct 10 '22 05:10

theabraham


Your example updated on jsFiddle

$("input[type=submit]").click(function(){
    $("<li />").html("item").appendTo("ul");
})

You can create elements using $("<tag />") and set attributes, add classes and so on. Then append where you want.

like image 40
BrunoLM Avatar answered Oct 10 '22 07:10

BrunoLM