PDA

View Full Version : Change value of a select list with a link (onClick)?


viktor
10-02-2002, 10:38 PM
Hello! I would like to change the value of a selectlist, with an onclick event in a textlink.

Something like this: <a href="#" onclick="document.theform.theselectlist.value='newvalue'">click here!</a>

...and when that link is clicked, the select list should go down to that value (which already exists in the list). Is this possible? It only has to work in IE 5.5+. Hope you can help!

ConfusedOfLife
10-02-2002, 10:54 PM
Try this :



<form>
<select name="Oopse">
<option value="a"> a
<option value="b"> b
<option value="c"> c
<option value="d"> d
</select>
</form>

<br /><br />
<a href="#" onclick="document.forms[0].Oopse.selectedIndex = 2"> Change it to C </a>



It's an ugly piece of code! I hope someone can write it more object oriented!

adios
10-03-2002, 12:06 AM
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function goToOption(sel, val) {
var opt, o = 0;
while (opt = sel[o++]) if (opt.value == val) sel.selectedIndex = o - 1;
}

</script>
</head>
<body>
<form name="elformo">
<select name="tester">
<option value="value3">value 3</option>
<option value="value7">value 7</option>
<option value="value12">value 12</option>
<option value="value4">value 4</option>
</select>
</form>
<a href="javascript:void goToOption(document.elformo.tester,'value12')">go to value 12</a>
</body>
</html>

I think that works! Let me know if it doesn't!

adios

whammy
10-03-2002, 01:48 AM
<form name="a_form_name">
<select name="ss1">
<option value="one">one</option>
<option value="two">two</option>
</select>
</form>

<a href="#" onclick="document.a_form_name.ss1.selectedIndex=1">Second thingy</a>

viktor
10-03-2002, 07:22 AM
Many thanks! One more question: I would like to have the selectlist mentioned above ONLY changable through the link, and have a pop up window displayed, like this:

<select onclick="NewWindow(this.href,'popwin','200','200','no','pos4');return false">
<option></option>
</select>

That works (at least in IE5 which is enough), but how do I prevent the list from opening when a user clicks it? Again, thanks, hope you can help me with this one as well...

glenngv
10-03-2002, 07:32 AM
<select onclick="NewWindow("URLhere",'popwin','200','200','no','pos4');window.focus()">

this.href wont work since it is not an <a> tag.

adios
10-03-2002, 07:33 AM
http://thumb-2.image.altavista.com/image/243262702
Are you talkin' to me?

OK, well...are you sure you want a drop-down?

<select name="tester" onfocus="self.focus()">

Not too annoying. You'll need to explain more about the pop-up...