Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Button Onclick Event & Switch Statement

    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>

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    I think this should work:
    Code:
    <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>

  • #3
    New Coder
    Join Date
    Oct 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by RSH17; 11-07-2004 at 07:30 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •