...

View Full Version : Button Onclick Event & Switch Statement



RSH17
11-06-2004, 04:42 PM
Reference: IE 6 & JavaScript

How do I code the onclick event of a button to execute a switch statement?
I wish to use the onclick event of button4 to execute a switch statement to change the value of button1.

<Language=JavaScript>

<INPUT type="button" value="Castings" id=button1 name=button1 >
<INPUT type="button" value="Raw Materials" id=button4 name=button4 onclick = "Raw Material">

<script>

Article = document.MaterialSelector.button4.value

switch (Article) {

case "Raw Material":
document.MaterialSelector.button1.value
break;

default:
break;

}

</script>

Brandoe85
11-06-2004, 09:17 PM
I think this should work:


<head>
<script language="javascript">
function change()
{
var Article = document.MaterialSelector.button4.value;

switch(Article)
{
case "Raw Materials":
document.MaterialSelector.button1.value = "Whatever";
break;
}

}


</script>
</head>
<body>
<form action="" name="MaterialSelector">
<input type="button" name="button1" value="Castings"><br>
<input type="button" name="button4" value="Raw Materials" onclick="change();"><br>
</form>

RSH17
11-06-2004, 10:11 PM
The code above works (Thank you).

After I looked at the above code for awhile, I see that I'm not quite there yet. I shall modify the code so it better represents what I wish to accomplist.

Here is the modified code:

<head>
<script language="javascript">
function change()
{
var Article = document.MaterialSelector.b2.value;

switch(Article)
{
case "Test2":
document.MaterialSelector.b1.value = "Whatever";
break;
}

}


</script>
</head>
<body>
<form action="" name="MaterialSelector">
<input type="button" name="b1" value="Test1" onclick="change();"><br>
<input type="button" name="b2" value="Test2" onclick="change();"><br>
<input type="button" name="b3" value="Test3" onclick="change();"><br>
<input type="button" name="b4" value="Test4" onclick="change();"><br>
</form>

Here is the issue that needs to be resolved. "var Article" is getting its' variable from: b2.value. I need "var Article" to get its' variable from whatever button is clicked on.

How do I code for this to occur?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum