Skip to main content
gamedev.net
🔒 Locked

[web] Javascript & select boxes help

Started by agm_ultimatex Jan 24, 2008 at 2:13 PM 3 replies 600+ views
Original Post
agm_ultimatex
agm_ultimatex
I'm working on having a select box with two options (yes and no). I wanna use &#106avascript so when the select box drops down, and someone clicks yes, an event happens, and when the user clicks no, a different even happens. Heres my code atm. (The i value being passed is to detect which select box it is as I have multiple). function nameOfFunction(i) { var start = document.getElementById("start"+i); var end = document.getElementById("end"+i); if(document.form1.available[0].text = "Yes") { alert("Yes value"); } if(document.form1.available[1].text = "No") { alert("No value"); } } What currently happens is no matter what value I click on in the select, both alerts appear. Thanks
NiteLordz
NiteLordz
2 issues.

1) the if statement is not a conditional x = 1, sets the value. virutally what you are saying is

if document.form1.available[0].text = "Yes" // your setting the value, always true
then you set an alert box.

what you want is

if document.form1.available[0].text == "Yes"
alert box

little tip for future reference,

if "Yes" == document.form1.available[0].text = "Yes"

in doing it this way, if you use

if "Yes" = document.form1.available[0].text = "Yes" this will throw an error, as "Yes" is not a variable.


not sure what your code is supposed to do here?
var start = document.getElementById("start"+i);
var end = document.getElementById("end"+i);

Code makes the man
agm_ultimatex
agm_ultimatex
I tried your version there, with the double = sign. Which makes more sense, however it still doesn't work. I also realized my handler event for the select box was onClick. I changed it to onChange, which made it smoother for my older code that set the value.

To answer your question, the vars start and end just get two table data elements. Or the one in the current row. As I have an asp loop that creates a table row containing this yes and no box, and two other select boxes. The start and end are the tds that contain each of those two other boxes. I'm just not at the stage in the script to utilize them yet.
agm_ultimatex
agm_ultimatex
I have it working now :)

Here is the code:
var availableIndex = document.getElementById("available").selectedIndex;
var availableValue = document.getElementById("available")[availableIndex].value;
if(availableValue == "y")
{
alert(availableValue);
}
if(availableValue == "n")
{
alert(availableValue);
}
Kylotan
Kylotan
Moving to Web Development.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.