We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
  • Pages:
  • 1
JavaScript: Changing a variables value based; on the selection in a drop-down list
Topic Started: Jun 16 2005, 07:26 PM (1,278 Views)
Postulate-ZNS
I am, therefore I think: Descartes can shove it
[ *  *  *  *  * ]
For example, if you had a variable:

Code:
 
var number = "4"

...and a drop down list containing two selections: "6" and "7." Selecting "6" would change the variable's value to 6, while selecting "7" would change it to 7.

I hope you see what I mean. Any help would be greatly appreciated. :)
Offline Profile Quote Post Goto Top
 
Stefan
Member Avatar
Mew?
[ *  *  *  *  *  *  * ]
Code:
 
<select onchange="someVariable = this.options[this.selectedIndex].value;">
<option value="6">6</option>
<option value="7">7</option>
</select>

That's pretty much it. ^_^
Offline Profile Quote Post Goto Top
 
Postulate-ZNS
I am, therefore I think: Descartes can shove it
[ *  *  *  *  * ]
Ah, thanks a bunch. :yes:

EDIT: Oh, and one more quick related question, while we're on the subject, for anyone who may know: is it possible to change multiple variables with one selection by repeating the code or adding to it? Thanks! ^_^
Offline Profile Quote Post Goto Top
 
JoeC
Euch! IE tastes horrible!
[ *  *  *  *  * ]
Yes.

Code:
 
<select onchange="someVariable = this.options[this.selectedIndex].value; other.things.tochange=[go.here]; each.item.must=[seperated.by.semicolon]">
<option value="6">6</option>
<option value="7">7</option>
</select>
Offline Profile Quote Post Goto Top
 
Postulate-ZNS
I am, therefore I think: Descartes can shove it
[ *  *  *  *  * ]
Thank you. :)

EDIT: Forgive me for being an idiot, but...

Code:
 

<select onchange="someVariable = this.options[this.selectedIndex].value;">
<option value="6">6</option>
<option value="7">7</option>
</select>


What exactly am I supposed to replace "this.options" and "this.selectedIndex" with? :unsure:
Offline Profile Quote Post Goto Top
 
Stefan
Member Avatar
Mew?
[ *  *  *  *  *  *  * ]
The only thing you would have to replace is someVariable, with your variable name.

The this in this.options and this.selectedIndex refers to the dropdown itself. options refers to its options and selectedIndex refers to the currently selected item.
Offline Profile Quote Post Goto Top
 
Postulate-ZNS
I am, therefore I think: Descartes can shove it
[ *  *  *  *  * ]
This is how I have it set up:
Code:
 
<select name="namehere" onchange="variable =
this.options[this.selectedIndex].value;">
<option value="50">Option 1</option>
<option value="5">Option 2</option>
</select>



And it isn't working :unsure: I have the variable displayed below, and it doesn't change with selection.

Sorry for all the questions. >_<
Offline Profile Quote Post Goto Top
 
Gornakle
Member
[ *  *  *  *  * ]
Pojo
June 17, 2005 05:22 PM
This is how I have it set up:
Code:
 
<select name="namehere" onchange="variable =
this.options[this.selectedIndex].value;">
<option value="50">Option 1</option>
<option value="5">Option 2</option>
</select>



And it isn't working :unsure: I have the variable displayed below, and it doesn't change with selection.

Sorry for all the questions. >_<

How did you display it? If you simply document.write()ed it, it's static. It's written to the screen once and won't update when the variable changes.
Offline Profile Quote Post Goto Top
 
Postulate-ZNS
I am, therefore I think: Descartes can shove it
[ *  *  *  *  * ]
I have it displayed in a text field, with a "document.forms...etc." JavaScript block at the end of the document to insert the variable in a text field. Will it still remain static? And if so, is there any other way to change the value of a text filed based on a select list option? :unsure:
Offline Profile Quote Post Goto Top
 
Postulate-ZNS
I am, therefore I think: Descartes can shove it
[ *  *  *  *  * ]
I hate to bump this, but i would like an answer. It's not too urgent, but I do have to finish this script soon.

Thanks to Stefan, Gornakle, and JoeC for helping me previously. :arr:
Offline Profile Quote Post Goto Top
 
solinent
Member
[ *  *  * ]
I've tested this one.

Allways use variable names that aren't really that normal, because the name variable is probably not allowed ;)

here:
Code:
 

<html>
<head>
<script language="javascript" type="text/javascript">
<!--//
function changeop() {
newvar = document.form1.namehere.value
document.form1.changed.value = newvar
}
//-->
</script>
</head>
<body>
<form name="form1">
<select name="namehere" onchange="changeop()">
<option value="50">Option 1</option>
<option value="5">Option 2</option>
</select>
<input type="text" name="changed">
</form>
</body>
</html>
Offline Profile Quote Post Goto Top
 
Postulate-ZNS
I am, therefore I think: Descartes can shove it
[ *  *  *  *  * ]
Thanks a lot, I'll be sure to try it out! :)
Offline Profile Quote Post Goto Top
 
Postulate-ZNS
I am, therefore I think: Descartes can shove it
[ *  *  *  *  * ]
I got it to work perfectly, but another issue arises: is it possible to have two variables changed by selecting an option that shows in two text fields? So, if I have two text input areas in a form, can it be made so that a different value shows in each one (i.e., one that says "3" and another that says "4") by selecting just one option? Again, thanks so much for your help! :)
Offline Profile Quote Post Goto Top
 
solinent
Member
[ *  *  * ]
Should work:(I'm not sure that's what you mean but...)

Code:
 


<html>
<head>
<script language="javascript" type="text/javascript">
<!--//
function changeop() {
newvar = document.form1.namehere.value;
document.form1.changed.value = newvar;
document.form1.changed2.value = newvar*10;
}
//-->
</script>
</head>
<body>
<form name="form1">
<select name="namehere" onchange="changeop()">
<option value="50">Option 1</option>
<option value="5">Option 2</option>
</select>
<input type="text" name="changed">
<input type="text" name="changed2">
</form>
</body>
</html>
Offline Profile Quote Post Goto Top
 
Postulate-ZNS
I am, therefore I think: Descartes can shove it
[ *  *  *  *  * ]
Thank you for your input, I'll try this out. If it doesn't work, I'll be sure to explain myself more clearly if you're still in the mood to help out. :)

EDIT: Not quite what I'm looking for, but oh so close. What I want is so that based on which selection in the drop-down is made, two different fields show two different specific numbers (sorry, I didn't make that clear). The numbers have no relation to each other; they have to be clearly defined. Is this possible?

(If you need more of an explanation, please ask, as sometimes I have difficulty explaining myself clearly).

Thank you so much!
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
Go to Next Page
« Previous Topic · Technology Chat · Next Topic »
Add Reply
  • Pages:
  • 1