Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting options in Netscape

Status
Not open for further replies.

zippynz

Programmer
May 17, 2001
50
0
0
NZ
Ok I am using the following script to select an option from a select list in IE, what I would like to know is how to get it to work in Netscape

This script is run from a spawned window, it either has a date passed to it or if there is not date it grabs todays date and uses that (what it is doing here)

var d = new Date();
window.opener.document.UpdateSuburb.dayStarted.value = (d.getDate());

It is to select the day of the month from this select box:

<select name=&quot;dayStarted&quot;>
<option value=&quot;1&quot;>1</option>
<option value=&quot;2&quot;>2</option>
etc...
</select>

Anyone have any ideas on this one?
thanks :)
 
Hmm.. I'm surprised that IE even lets you do that. At any rate, the value property is a settable property in JavaScript, it can only be set through the options index. I've yet to see someone try to set a value to the actual select list. But at any rate, as you can see, NS won't parse that script properly.

According to my JS reference, the only way to change the current selection in a select list dynamically is to use the document.selectlist.options[n].selected = true. This will deselect the current option and select the option indicated by the options[n] index number.

Now, you're particular need is quite challenging because it's possible that you will not know the options index number that you want to change the list to. In your example above, what you want to give JavaScript is the value and you want JavaScript to select the option in the list that contains that value.

To do this, you're going to have to tell JavaScript to loop through every option in the list until it finds the value you're looking for, then select that option.

I've created a ready to run sample page that shows you exactly how this is done. Now, I tried to copy as much of your convention and element names into this example, but of course, I am not referencing an opener window and I am not using a date object to set the search value. Nonetheless, you should be able to see here how it's done.

There's a small onLoad event handler function that sets the Text in the button just for the purpose of this test. You can ignore that as part of your solution.

Basically, change the value of the var d = &quot;n&quot; setting. Reload the browser and click on the button. You'll see JavaScript will dynamically set the list in code.

Works in both IE and Netscape

Code:
<html>
<head>
<script Language=&quot;JavaScript&quot;>
<!--

var d = &quot;4&quot;

function changeList() {
  for (var m = 0; m < document.UpdateSuburb.dayStarted.length; m++) {
    if (document.UpdateSuburb.dayStarted.options[m].value == d) {
      document.UpdateSuburb.dayStarted.options[m].selected = true
    }
  }
}

function setButtonText() {
  document.UpdateSuburb.change.value = &quot;Change List To &quot; + d
}

//-->
</script>
</head>
<body onLoad=&quot;setButtonText();&quot;>
<form name=&quot;UpdateSuburb&quot;>
<select name=&quot;dayStarted&quot;>
  <option value=&quot;1&quot;>1 </option>
  <option value=&quot;2&quot;>2 </option>
  <option value=&quot;3&quot;>3 </option>
  <option value=&quot;4&quot;>4 </option>
  <option value=&quot;5&quot;>5 </option>
  <option value=&quot;6&quot;>6 </option>
</select><br><br>
<input name=&quot;change&quot; type=&quot;button&quot; value=&quot;&quot; onClick=&quot;changeList()&quot;>
</form>
</body>
</html>

Hope this helps :)

Let me know if you get hung up or have any questions.

ToddWW
 
thanks a bunch,
I will try that out tomorrow :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top