Solved

Can an <option>  tag be dynamically modified?

Posted on 1999-01-21
6
175 Views
Last Modified: 2010-04-09
Can the following be done:
An html page contains radio buttons and an option tag (combo box).  If you click one of the radio buttons, a javascript is called that will modify the content (the pulldown options) in the option tag dynamically without reloading a new page.  Can it be done? if so, what is the code (in JVScript or html) that will modify the content of the option dynamically.
0
Comment
Question by:matala
6 Comments
 
LVL 6

Expert Comment

by:PBall
ID: 1845580
Yes, but it might not work on some older browsers like IE3 (I think)

<select name=selAnswers>
<option value='a'>16 meters
<option value='b'>12 meters
<option value='c'>14 meters
<option value='d'>other

<script>
document.selAnswer.options[0] = new Option('a','New York')
document.selAnswer.options[1] = new Option('b','Chicago')
document.selAnswer.options[2] = new Option('c','Dallas')
document.selAnswer.options[3] = new Option('c','San Francisco')
</script>

Hmm..I forgot how to delete the previous options tho, hmm...
anyone?
0
 
LVL 4

Expert Comment

by:martinag
ID: 1845581
The list has got to be filled with x empty options where x is the number of options that will ever be used.
Then, you dynamically assign text and value
<SELECT>
<OPTION>
<OPTION>
<OPTION>
</SELECT>
[...]
// A list
list.options[0].value = "soccer";
list.options[0].text = "Soccer";
list.options[1].value = "basketball";
list.options[1].text = "BasketBall;
list.options[2].value = "hockey";
list.options[3].text = "Hockey";
[...]
// Another list
list.options[0].value = "y";
list.options[0].text = "Yes";
list.options[1].value = "n";
list.options[1].text = "No";
list.options[2].value = ""; // Empty, or it will be "hockey"
list.options[3].text = ""; // Empty, or it will be "Hockey"

Martin
0
 
LVL 4

Expert Comment

by:martinag
ID: 1845582
When you do like PBall suggests you have to reload for the changes to be made.

Martin
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:martinag
ID: 1845583
Oops.
  list.options[2].value = ...
  list.options[3].text = ...
should of course be
  list.options[2].value = ..
  list.options[2].text = ...

Martin
0
 

Author Comment

by:matala
ID: 1845584
Thanks, it works with netscape, but not with iexplorer.  How can I make it work in both.
The error in iexplorer 4.0 is "...options is not an object"
0
 
LVL 11

Accepted Solution

by:
mouatts earned 50 total points
ID: 1845585
Firstly it should  option not options.
Secondly to remove all element from the list you can use option.length=0
Thus you only need to put the options that need to be there and don't need 'empty' ones to fill the gaps.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jump to a specific point on a responsive page 5 27
Disabled form text field 2 22
Basic JavaScript Question 3 37
Need to click button twice to get results 2 32
Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now