FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

select boxes value change?

 


alalex
Can you change the value of a select box using javascript?

For example, I have a form called 'bulbs', and in it, I have a select box that appears as the fifth select box. Can I use the following code to change it from unselected to selected:
Code:

<script language="javascript" type="text/javascript">
document.bulbs.options[4].selected = true;
</script>
bladesage
alalex wrote:
Can you change the value of a select box using javascript?

For example, I have a form called 'bulbs', and in it, I have a select box that appears as the fifth select box. Can I use the following code to change it from unselected to selected:
Code:

<script language="javascript" type="text/javascript">
document.bulbs.options[4].selected = true;
</script>


Try using
Code:
document.bulbs.options.selectedIndex = 4;


This works for select boxes, and it is indexed in the normal javascript fashion (0,1,2,3,4,5...), so you just use the selectedIndex ability to edit the selection. You can use document.bulbs.options.value to see what is selected, but that's about it.

If you're actually talking about really selecting the box, use
Code:
document.bulbs.options.focus();
document.bulbs.options.select();


Or in the options list,
Code:
<option id="opt1">1
<option id="opt2">2
<option id="opt3">3


And you can then select them using
Code:
document.getElementById("opt1").focus();
document.getElementById("opt1").select();
alalex
all right... thanks! Very Happy
bladesage
alalex wrote:
all right... thanks! Very Happy


No problem, I love javascript.
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.