Im always used to using jquery or other javascript frameworks but right now I have no frameworks to use so i wanted to see if anyone knows how i can do this in straight javascript if possible.
Basically i have a div like this
<input type="button" id="more_fields" onclick="add_fields();" value="Add More" />
<div id="room_fileds">
<div class='label'>Room 1:</div>
<div class="content">
<span>Width: <input type="text" style="width:48px;" name="width[]" value="" /><small>(ft)</small> X </span>
<span>Length: <input type="text" style="width:48px;" namae="length[]" value="" /><small>(ft)</small</span>
</div>
</div>
What i need one is when the add more button is clicked i need to basically add more width and length fields either creating a entire structure like the one above with all the divs or just inserting new span tag with the fields below the existing ones.
I know how to do it with jquery or prototype framework but unfortunatley I cannot use any frameworks for this. Does anyone have any idea how to do it. I would post code iv done for this but I dont even know where to beging.
You can use the
innerHTML
property to add content. Add aid="wrapper"
to thediv
surrounding yourspan
elements and then you can doOf course you don't need the
id
and can use other DOM methods to get to thediv
, but I find usingid
s easier and cleaner. Here's a quick fiddleAlso note that you shouldn't inline your css code, neither attach your javascript calls directly inside the DOM elements. Separating DOM, Javascript and CSS will make your life easier.
Don't need create new room?.
Demo: http://jsfiddle.net/nj4N4/7/
just use the
innerHTML
like this:btw I changed div
class="content"
toid="content"
you can add new id if you prefer.DEMO: http://jsbin.com/oxokiq/5/edit