2 Replies - 753 Views - Last Post: 14 February 2012 - 11:07 PM

#1 Polofiesta   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 15-December 10

Adding elements to a div.

Posted 13 February 2012 - 01:19 PM

Hi I am making a function on my website where a person can click a button and it will add another form for them so they can list more things to sell at once. I am wondering how can i accomplish this through javascript. I understand the dom but i only see to add text to elements etc. I need to add something like this:
<label class = "label">Title:</label>
					<input type = "text" size = 50>

to a div like this:


How do i put that inside of the div?

Is This A Good Question/Topic? 0
  • +

Replies To: Adding elements to a div.

#2 cfrostNCH   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-October 11

Re: Adding elements to a div.

Posted 13 February 2012 - 01:23 PM

Give the div an id and use innerHTML.

document.getElementById('myID').innerHTML = '<label class = "label">Title:</label><input type = "text" size = 50>'

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5226
  • View blog
  • Posts: 14,005
  • Joined: 18-April 07

Re: Adding elements to a div.

Posted 14 February 2012 - 11:07 PM

You may also want to investigate the method appendChild. By giving your div an id and then calling appendChild on that element through the DOM you can dynamically create and append other elements.

For instance this code would create a text node and append it to the body of the page.

var newText = document.createTextNode("This is a text item");

After this code executes, the line of text would magically appear on the page. You can append your label and input elements there to the div using this method.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1