Basic buttons
Default behavior | Code - clicking the text does nothing |
---|---|
%RH Over Water %RH Over Ice Mixing Ratio (ppm) |
%RH Over Ice Mixing Ratio (ppm) |
Using the *label* tag
In order to associate the text with the buttons (ie, make it clickable), use the label tag. I have verified that it works in IE8 and Chrome 49.
Desired behavior | Code using <label> tags |
---|---|
! added 05-23-19 > The tags must be closed in most other cases.
Adding IDs
Desired behavior |
---|
%RH Over Water
%RH Over Ice Mixing Ratio (ppm) |
%RH Over Water %RH Over Ice Mixing Ratio (ppm) |
Code that makes it work
window.onload = function() { AssociateRadioButton('rg1_0'); // called for each button AssociateRadioButton('rg1_1'); AssociateRadioButton('rg1_2'); } // This configures the identifying string's click event so that // the associated radio button is selected (checked) when the identifying string is clicked function AssociateRadioButton(idName) { document.getElementById(idName+'t').addEventListener("click", function() { document.getElementById(idName).checked = true;} ) ;} |
getElementsByName
window.onload = function() { mcInitRadioButtons('rg1'); } function mcInitRadioButtons(groupName) { var RadioGroup = document.getElementsByName(groupName); for(var i = 0; i < RadioGroup.length; i++) { var Radio_ID = RadioGroup[i].id; var Text_Element = document.getElementById(Radio_ID+'t'); if (Text_Element ) { Text_Element.addEventListener("click", function() { RadioGroup[i].checked = true; } ); } } } |
That code should have worked! However .. RadioGroup[i] was not evaluated. Instead, it was placed in the function as typed. Therefore, when the text was clicked, i was equal to 3. It did not matter which of the 3 lines of text was clicked, none of them worked!
Next I tried this - but all 3 elements pointed to the last radio button.
function mcInitRadioButtons(groupName) { var RadioGroup = document.getElementsByName(groupName); for(var i = 0; i < RadioGroup.length; i++) { var Radio_ID = RadioGroup[i].id; var Radio_Element = RadioGroup[i]; var Text_Element = document.getElementById(Radio_ID+'t'); if (Text_Element ) { Text_Element.addEventListener("click", function() { Radio_Element.checked = true; } ); } } } |
Javascript sucks !!!
The problem is do to something called Closure. This is complete nonsense. Who designs a language like this without a way to overcome this serious limitation.
At any rate, the following code works. It is not as efficient as I was trying to be, but it works!
function mcAssociateRadioButton(idName) { document.getElementById(idName+'t').addEventListener("click", function() { document.getElementById(idName).checked = true;} ) ; } function mcInitRadioButtons(groupName) { var RadioGroup = document.getElementsByName(groupName); for(var i = 0; i < RadioGroup.length; i++) { var Radio_ID = RadioGroup[i].id; var Text_Element = document.getElementById(Radio_ID+'t'); if (Text_Element ) { mcAssociateRadioButton(Radio_ID); } } } |
window.onload = function() { mcInitRadioButtons('rg1'); // call with the name of the radio group } // This configures the identifying string's click event so that // the associated radio button is selected (checked) when that string is clicked function mcAssociateRadioButton(idName) { var UIElement = document.getElementById(idName); document.getElementById(idName+'t').addEventListener("click", function() { UIElement.checked = true;} ) ; } function mcInitRadioButtons(groupName) { var RadioGroup = document.getElementsByName(groupName); for(var i = 0; i < RadioGroup.length; i++) { var Radio_ID = RadioGroup[i].id; var Text_Element = document.getElementById(Radio_ID+'t'); if (Text_Element ) { mcAssociateRadioButton(Radio_ID); } } } |
Determining which option is selected
The code below contains 2 functions - they both return the value assigned to the checked Radio Button in the specified Radio Group (the group of Radio Buttons with the same name).
switch (mcRadioGroupIndex("rg1")) { case 0: func_0(); break; case 1: func_1(); break; case 2: func_2(); break; } // mcRadioGroupIndex(groupName) returns a number - values should be integers // mcRadioGroupValue(groupName) returns a string - for non-integer values function mcRadioGroupIndex(groupName) { return mcRadioGroupValue(groupName)*1; // convert a string to a number } function mcRadioGroupValue(groupName) { var result = 0; var RadioGroup = document.getElementsByName(groupName); for(var i = 0; i < RadioGroup.length; i++) { if(RadioGroup[i].checked == true) { result = RadioGroup[i].value; } } return result; } |
Setting an Option
function mcRadioGroupSetValue(groupName, value) { var val = String(value); // allows numbers or strings var RadioGroup = document.getElementsByName(groupName); for(var i = 0; i < RadioGroup.length; i++) { if(RadioGroup[i].value == val) { RadioGroup[i].checked = true; } } } |
Group border
Group name
%RH Over Water %RH Over Ice Mixing Ratio (ppm) |
|
|
onclick
Firefox Opera |
The question is - Is there a way to assign a function without modifying each button?
Perhaps fieldset would work.
|