...

View Full Version : Declaring variable with onClick trouble



elchoco
05-23-2006, 02:27 PM
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.



<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:


<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.

Mr J
05-24-2006, 12:03 AM
The words color and style can be assumed to be reserved words


Give this a try



<script type="text/javascript">

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

function colorStyle(){

myForm=document.specifics

chosenColor=""

for(var i=0; i<myForm.myColor.length; i++){
if(myForm.myColor[i].checked){
chosenColor= myForm.myColor[i].value
}
}

chosenStyle=""

for(var j=0; j<myForm.myStyle.length; j++){
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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum