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 2 of 2
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Declaring variable with onClick trouble

    I'm trying to go to a specific URL in accordance with user (radio) button selecting.
    Currently I have code that will pass it to a text input box. I don't see why I can't use color and style as variables. Any help would make me the happiest man in Wyoming.


    Code:
    <script>
    var color=""; var style="";
    function colorstyle(which){
    j=which.color.length;
    for (i=0; i<j; i++){
    if(which.color[i].checked)
    var color = which.color[i].value
    }
    j=which.style.length;
    for (i=0; i<j; i++){
    if(which.style[i].checked)
    var style = which.style[i].value
    }
    which.buttons.value = (color+', '+style)
    }
    </script>
    BODY:
    Code:
    <form method="get" onSubmit="return OnSubmitCheck();" action="../buyRedirect.htm" name="specifics">
     <div onClick="colorstyle(document.specifics);">
    
    Blue
    <input type="radio" name="color" value="blue">
     Red
     <input type="radio" name="color" value="red">
    
    Standard
    <input type="radio" name="style" value="stan">
     Tuck-in
     <input type="radio" name="style" value="tuck">
    </div><input type="submit" id="submit1" value="Buy Now">
     <script type="text/javascript">
     document.write("This is color: " +color);
    </script>
     <input type=text name="buttons" size=30>
    </form>
    again any help would be wonderful. Thank you in advance.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    The words color and style can be assumed to be reserved words


    Give this a try

    PHP Code:
    <script type="text/javascript">

    var 
    myColor="";
    var 
    myStyle="";

    function 
    colorStyle(){

    myForm=document.specifics

    chosenColor
    =""

    for(var i=0i<myForm.myColor.lengthi++){
    if(
    myForm.myColor[i].checked){
    chosenColormyForm.myColor[i].value
    }
    }

    chosenStyle=""

    for(var j=0j<myForm.myStyle.lengthj++){
    if(
    myForm.myStyle[j].checked){
    chosenStyle myForm.myStyle[j].value
    }
    }

    myForm.buttons.value = (chosenColor+', '+chosenStyle)

    }
    </script>

    <form method="get" onSubmit="return OnSubmitCheck();" action="../buyRedirect.htm" name="specifics">

    <div onclick="colorStyle()">
    Blue <input type="radio" name="myColor" value="blue"> 
    Red <input type="radio" name="myColor" value="red"> 
    Standard<input type="radio" name="myStyle" value="stan"> 
    Tuck-in <input type="radio" name="myStyle" value="tuck"> 
    </div>

    <input type="submit" id="submit1" value="Buy Now">
    <script type="text/javascript">
     document.write("This is color: " +myColor);
    </script>
    <input type=text name="buttons" size=30>
    </form> 
    Last edited by Mr J; 05-23-2006 at 11:07 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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