PDA

View Full Version : Change Var



Tabo
04-22-2007, 11:08 AM
I have a Var, lets call it Var1

How can i change the value of that var by using a dropdown box?

Thanks

pcbrainbuster
04-22-2007, 11:33 AM
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 _

mwinter
04-22-2007, 11:51 AM
How can i change the value of [a variable] by using a dropdown box?

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:



<select onchange="var1 = 'foo';">

which replaces the value of the variable, var1, with the string, 'foo', when the selection of the element changes.



1. The variable to be edited must be global

The variable must be in scope. That doesn't necessarily mean global.



2. Variable names can't start with numbers and only can with a letter or _

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.)

Mike

Bob90
04-23-2007, 09:29 AM
Talk about killing the newbie.
New coders want someting that works, like a cookbook piece that is clearly explained.



<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>


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:...."
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 the
;alert(foo)
allows you to see what value you are giving to foo. just remove that to make the script working silently

mwinter
04-24-2007, 09:04 PM
New coders want someting that works, like a cookbook piece that is clearly explained.

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.



<select onChange="Javascript:foo = this.options[this.selectedIndex].value; alert(foo)">

That "Javascript:" prefix is unnecessary. Omit it.



var foo = "";

There doesn't seem much point in initialising the variable.

Mike