467,926 Members | 1,396 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

How to add new options in select options using JavaScript

how to add new more options in select box options using JavaScript . Please tell me what i do for add more options in options box using JavaScript .
Apr 10 '13 #1
  • viewed: 5417
Share:
5 Replies
8TB
Try

Expand|Select|Wrap|Line Numbers
  1. var newOption= document.createElement("OPTION");
  2. newOption.innerText = "yourText";
  3. newOption.value = "yourValue";
  4. document.yourForm.yourSelect.options.add(newOption);
Apr 10 '13 #2
acoder
Expert Mod 8TB
That looks like a very IE-specific way of adding options.

I would suggest a simple:
Expand|Select|Wrap|Line Numbers
  1. yourSelect.options[yourSelect.options.length] = new Option(text, value);
If using the add method, you'd need a try/catch block to deal with IE's non-standard implementation (probably been fixed in newer versions, but I haven't checked).
Apr 10 '13 #3
8TB
Not to mention that
Expand|Select|Wrap|Line Numbers
  1. options[yourSelect.options.length] = new Option(text, value);
is much cleaner and simpler ...
Apr 10 '13 #4
64KB
Try This Code

Expand|Select|Wrap|Line Numbers
  1. var min = 10,
  2.     max = 50,
  3.     select = document.getElementById('selectElementId');
  4.  
  5. for (var i = min; i<=max; i++){
  6.     var opt = document.createElement('option');
  7.     opt.value = i;
  8.     opt.innerHTML = i;
  9.     select.appendChild(opt);
  10. }
3 Weeks Ago #5
2Bits
I have the same problems. Thanks for share this.
2 Weeks Ago #6

Post your reply

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

Similar topics

2 posts views Thread by simon.wilkinson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.