File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dynamically adding elements to another using javascript

 
Rajesh Khan
Ranch Hand
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i wanted to add attributes to a list box dynamically , say I have a list box as such


now with java script function how can I add another item to the list box dynamically. I know .setAttribute() wont do the trick . I actually want to add <option> attributes dynamically such as


any suggestions on how i could accomplish this??
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Options are elements not attributes. You need to use the DOM manipulation API to create the new element and attach it to the DOM at the correct location.

Investigate methods such as document.createElement() and document.createTextNode(), as well as the appendChild() method of Element.

But to be honest, life is too short to fuddle with the DOM using the native JavaScript API. Using a library such as jQuery makes life so much simpler:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic