I have a Var, lets call it Var1
How can i change the value of that var by using a dropdown box?
Thanks
I have a Var, lets call it Var1
How can i change the value of that var by using a dropdown box?
Thanks
I don't know what you want but lets give you an example -
<script>
var a = "ape"
</script>
This creates a variable called a (as numbers are not aloud to be first in a name) and it has the value of ape...
<script>
var a = "ape"
function changevar() {
a = "monkey"
}
</script>
All you have to do to change the value is re-write the variable without the var and viola its over-written, a few notes though -
1. The variable to be edited must be global
2. Variable names can't start with numbers and only can with a letter or _
Last edited by pcbrainbuster; 04-22-2007 at 11:44 AM.
Attach a change event listener to the select element, the perform the assignment when the listener is called.
A simplistic (and rather useless) example would be:
which replaces the value of the variable, var1, with the string, 'foo', when the selection of the element changes.HTML Code:<select onchange="var1 = 'foo';">
The variable must be in scope. That doesn't necessarily mean global.
Or a dollar symbol ($), though its use is reserved: "The dollar sign is intended for use only in mechanically generated code." (7.2, ECMA-262 3rd ed.)2. Variable names can't start with numbers and only can with a letter or _
Mike
Talk about killing the newbie.
New coders want someting that works, like a cookbook piece that is clearly explained.
When the select box is changed the onchange event is called. Instead of calling a function, I deal with all the javascript in the select tag by using "Javascript:...."Code:<select onChange="Javascript:foo = this.options[this.selectedIndex].value; alert(foo)"> <option value="bar" selected>Value to display</option> <option value="bar 2">Value to display</option> <option value="bar 3">Value to display</option> <option value="bar 4">Value to display</option> <option value="bar 5">Value to display</option> </select> <script> var foo = ""; </script>
The Javascript changes the variable foo to the value of the select box's option that is selected.
this.options[] = the select box's options
this.selectedIndex = the index (or order) of the options
so this.options[3].value would always set the value of foo to bar 4! As index starts at 0!
Hope this helps explain things
ps theallows you to see what value you are giving to foo. just remove that to make the script working silentlyCode:;alert(foo)
Which isn't possible if enough information to provide a definite answer isn't given. The OP can always come back and ask again and provide more information.
Dishing out answers on a plate doesn't always help. It can just leave the person asking the question dependent upon further help. Whenever possible, I prefer to point people in the right direction and develop things on their own. If they aren't capable of that, they only need say.
That "Javascript:" prefix is unnecessary. Omit it.<select onChange="Javascript:foo = this.options[this.selectedIndex].value; alert(foo)">
There doesn't seem much point in initialising the variable.var foo = "";
Mike
Bookmarks