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

L

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?
 
M

Martin Honnen

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'));
}
 
L

Lee

(e-mail address removed) 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.type=="radio" && f.elements.checked) {
var v=f.elements.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>
 
L

lee

Thanks Lee, that's exactly what I needed.

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

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top