*
The moose likes HTML, CSS and JavaScript and the fly likes dynamically adding elements to another using javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "dynamically adding elements to another using javascript" Watch "dynamically adding elements to another using javascript" New topic
Author

dynamically adding elements to another using javascript

Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
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

Joined: Jan 10, 2002
Posts: 60808
    
  65

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:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamically adding elements to another using javascript
 
Similar Threads
How many attributes can a servlet have?
Selecting option in second selectOneMenu not setting property value
How to get last selected value in list box?
I want to fill a Listbox with values dynamically using JSP when I press a button
In List Highlighting previous selected values