By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,536 Members | 1,504 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,536 IT Pros & Developers. It's quick & easy.

Possible for javascript to handle 2 values from a radio button?

P: n/a
lee
Hello

I need some javascript code I've written to handle 2 values from an
HTML form.

I've been successful using the "label" attribute (I've simplied the
example below):

<input type="radio" name="group1" value="3" label="Cheese"
checked>Cheese<br>
<input type="radio" name="group1" value="5" label="Meat">Meat<br>
<input type="radio" name="group1" value="15" label="Bread">Bread<br>

But this only works in Internet Explorer, it will not work in
Mozilla/Firebox.

I've also tried id="" but that does not work.

Does anyone know how I can refence 2 values per radio button in
javascript?

Jul 29 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


le*@dnuk.com wrote:

I've been successful using the "label" attribute (I've simplied the
example below):

<input type="radio" name="group1" value="3" label="Cheese"
checked>Cheese<br>
<input type="radio" name="group1" value="5" label="Meat">Meat<br>
<input type="radio" name="group1" value="15" label="Bread">Bread<br>

But this only works in Internet Explorer, it will not work in
Mozilla/Firebox.
I don't see any script in that example so if anything does not work or
does not work as you want it why do you ask in a JavaScript group?

Does anyone know how I can refence 2 values per radio button in
javascript?


I am not sure I understand what you are looking for. Do you want to read
out the value of the label attribute of an element? Then use
element.getAttribute('label')
so assuming you have your radio button group inside of a
<form name="formName">
then you can access the first radio button as
var radioButton = document.forms.formName.elements.group[0];
if (radioButton && radioButton.getAttribute) {
alert(radioButton.getAttribute('label'));
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 29 '05 #2

P: n/a
Lee
le*@dnuk.com said:
Does anyone know how I can refence 2 values per radio button in
javascript?


<html>
<head>
<title>Two Values</title>
<script type="text/javascript">
function showSel(f) {
for (var i=0;i<f.elements.length;i++) {
if(f.elements[i].type=="radio" && f.elements[i].checked) {
var v=f.elements[i].value.split(',');
alert("\""+v[0]+"\" and \""+v[1]+"\"");
}
}
}
</script>
</head>
<body>
<form>
<input type="radio" name="group1" value="3,Cheese" checked>Cheese<br>
<input type="radio" name="group1" value="5,Meat">Meat<br>
<input type="radio" name="group1" value="15,Bread">Bread<br>
<input type="button" value="Show Selection" onclick="showSel(this.form)">
</form>
</body>
</html>

Jul 29 '05 #3

P: n/a
lee
Thanks Lee, that's exactly what I needed.

I've left the script off to keep the question simple.

Thanks.

Jul 29 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.