FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
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.
Related topics
Javascript and Dynamic Select Boxes
Dynamic SELECT boxes based on selections from other selects
Question About Drop-Down search buttons
managing a bilingual site
Speeding Up Starup Taskbar Items Loading...
counter strike scource: how to code a door
HTML form, list of options or text input
Windows Tips&tricks!
FIXED: javascript action on select element
Chained Selects
have problem on my sql
Site Nav Suggestions...
PHP Regex
mobile programmers codes
Joomla Publisher Issue
[solved] html multiply list doesnt give all values
How do I install Wordpress?
Submit buttons (is this PHP?)
[HTML][FORM]change the size of a input select
AJAX $_POST
changing form entry values before submit
searching users
Windows setup not detecting full drive size
pimp my drawing ^^
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.