By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Node.insertBefore

100+
P: 184
Hi All,
In a JSP I'm having like
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4. function Alter()
  5. {
  6.   var oldNode = document.getElementById("selectRows");
  7.   var optionElement = document.createElement("option");
  8.   optionElement .setAttribute("id","1");
  9.   optionElement .setAttribute("value","EE");
  10.   var optionText = document.createTextNode(newSelectText);
  11.   optionElement.appendChild(optionText);
  12.   // I need to append this 'optionElement' under 'oldNode' at 2nd position after "AA"
  13.  
  14.  
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <select id="selectRows">
  20.  <option id="1" value ="AA"> AA</option> 
  21.  <option id="2" value ="BB"> BB</option>
  22.  <option id="3" value ="CC"> CC</option>
  23.  <option id="4" value ="DD"> DD</option>
  24. </select>
  25. <input type="button" name="submit" value="submit" onClick="Alter()">
  26. </body>
  27. </html>
  28.  
  29.  
How to do I insert the "option element" under "old Node" at 2nd position after "AA".. Thanks in Advance..
Jul 14 '08 #1
Share this Question
Share on Google+
1 Reply


hsriat
Expert 100+
P: 1,654
Change your Alter function like this:
Expand|Select|Wrap|Line Numbers
  1. var sel = document.getElementById('selectRows');
  2.  
  3. for (var i = sel.options.length; i>=2; i--)
  4. sel.options[i] = new Option(sel.options[i-1].value, sel.options[i-1].innerText);
  5.  
  6. sel.options[1] = new Option('EE','EE');
Jul 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.