2

I have these inputs:

<input type='radio' id='1' name='s' onclick='pieces(this.value);'  value='6'>6
<input type='radio' id='2' name='s' onclick='pieces(this.value);'  value='12'>12
<input type='radio' id='3' name='s' onclick='pieces(this.value);'  value='24'>24
<input type='radio' id='11' name='v' onclick='pieces(this.value);'  value='yes'>Yes
<input type='radio' id='12' name='v' onclick='pieces(this.value);'  value='no'>No

I want to pass the value from "name='s'" to var1, and value from "name='v'" to var2 to the function pieces(var1, var2) when the radio button is clicked.

1
  • @arttronics, already editted. thanks Commented Jun 30, 2012 at 8:35

3 Answers 3

1

You can pass the arguments in the following different ways:

function antguider(name, url){
    alert("Name = "+name+" URL = "+url);
}

HTML Part

<input type="button" onclick=antguider('AntGuider','http://antguider.blogspot.com') value="Click Without Quotation" />
<input type="button" onclick="antguider('AntGuider','http://antguider.blogspot.com')" value="Click With Quotation" />
<input type="button" onclick="antguider(12345,'http://antguider.blogspot.com')" value="No Quotation Needed For Button" />
Sign up to request clarification or add additional context in comments.

7 Comments

You are missing double quotes for the first onclick attribute. This markup will not validate and can cause parsing issues.
If you have reference to using onclick in element tag without quotation for first input button, please provided it. Thank you.
In an HTML attribute without Quotation also work. Quotation is used to group. Example) title="Click Event" will work and title=Click Event will take the first value(Click) only. That is if a word doesn't contains space then no need of Quotation.
Why then does your non-spaced onclick fail W3C validation? Ok, I just found a great article that describes your Answer and why it fails validation. +1 for a great answer! Reference: article
It is not a correct way to use like that. But it is working in all the Major Browsers.
|
1

You can get values of both radio inputs in Javascript method itself using document.getElementsByName() method or this should also work,
Try using func(document.getElementsByName('s'),document.getElementsByName('v'))

Below code should help,

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="js1/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    alert('Ok');
    $('#btn').click(function(){
        var r1=$('input:radio[name=s]').val();
        var r2=$('input:radio[name=v]').val();
        show(r1,r2);
    });
});

function show(r1,r2){
    alert(r1);
    alert(r2);
}
</script>
</head>
<body>
<input type='radio' id='1' name='s' onclick='pieces(this.value);'  value='6'>6
<input type='radio' id='2' name='s' onclick='pieces(this.value);'  value='12'>12
<input type='radio' id='3' name='s' onclick='pieces(this.value);'  value='24'>24


<input type='radio' id='11' name='v' onclick='pieces(this.value);'  value='yes'>Yes
<input type='radio' id='12' name='v' onclick='pieces(this.value);'  value='no'>No

<button id="btn">Submit</button>
</body>
</html>

3 Comments

You can change show function to get values there also as shown below, function show(){ var r1=$('input:radio[name=s]').val(); var r2=$('input:radio[name=v]').val(); alert(r1); alert(r2); }
why that code doesn't work, i applied that to ajax and it seems like only the 1st variable (r1) got value. xmlhttp.open('GET','url.php?a1='+r1+'&a2='+r2,true);
@ivory-santos, this particular Answer requires that you choose two radio buttons as nothing is set from the beginning. Check out the previous comment with the jsFiddle I provided. Cheers!
0

For 2 inputs, this can easily be done with JQuery.

va1=$("input[name=s]").val();
va2=$("input[name=v]").val();

Then call the desired function, passing in your values as parameters.

func(va1,va2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.