Value from radiobuttons – a prototype.js way

Radiobuttons are occasionally useful, but they really seems to be made for being read out through the good old form submit. But if you want to read the value from java script?

With prototype.js linked in, it can be done this way:

function radiovalue(form,radioset){
	var rb=$(form)[radioset];
	var value;
	$A(rb).each(function(r){
		if(r.checked){
			value=r.value;
		}
	});
	return(value);
}

The form must have an id and the function is called with the id of the form and the name of the radiogroup, ie with the html

<form id="radioform">
<input type="radio" name="specttype" value="Sum" />Sum specter
<input type="radio" name="specttype" value="Det.1" />Detector 1
<input type="radio" name="specttype" value="Det.2" />Detector 2
</form>

the javascript

radiovalue('radioform','specttype') 

will return the value of the selected radiobutton.

This entry was posted in Data, Diverse, javascript. Bookmark the permalink.