14

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/

1
  • 2
    Yes, it is possible to create new DOM elements with JavaScript. Have you tried anything? There are plenty of jQuery tutorials out there. Commented May 2, 2011 at 1:21

5 Answers 5

17

Try this:

$('.button').click(function() {
    $('#myContainer').append('<div>the new guy</div>');
});
Sign up to request clarification or add additional context in comments.

Comments

12

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.

1 Comment

is there a way to make this animated?
4

You can add a new element to an existing parent like so:

select the element to added the new <div>/<li> to and use .append()

$("#id").append("<div>foo</div>");

http://api.jquery.com/append/

Alternatively, you can use the .html()

http://api.jquery.com/html/

Comments

1

If this is to allow for multiple file uploads, have you considered using something like http://www.uploadify.com/ the jQuery plugin? It allows multiple file uploads from one dialog window and you wouldn't need to worry about this.

Comments

0
$("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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.