Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
75
rated 0 times [  81] [ 6]  / answers: 1 / hits: 34790  / 11 Years ago, fri, november 22, 2013, 12:00:00

I'm writing a code to select a value from a drop-down list and the radio button will automatically checked itself according to the onClick attribute. The code worked both in Firefox and IE but not in Chrome.



This is the code for the drop-down list:



<select name=c_DOB_year id=c_DOB_year>
<option selected=selected>Year</option>
<option value=2008 onClick=document.getElementById('c_age_2').checked=true;>2008</option>
<option value=2009 onClick=document.getElementById('c_age_1').checked=true;>2009</option>
<option value=2010 onClick=document.getElementById('c_age_0').checked=true;>2010</option>
</select>


This is the code for the radio button:



<label>
<input type=radio name=c_age value=4 Years id=c_age_0 />
4 Years
</label>

<label>
<input type=radio name=c_age value=5 Years id=c_age_1 />
5 Years
</label>

<label>
<input type=radio name=c_age value=6 Years id=c_age_2 />
6 Years
</label>


What seems to be the problem? Hope someone can help me. Thanks. :D


More From » html

 Answers
15

I don't believe the click event is valid on options. It is valid, however, on select elements. Give this a try:



var d= {
'2008':'c_age_2',
'2009':'c_age_1',
'2010':'c_age_0',
};
var ele = document.getElementById('c_DOB_year');
ele.onchange = function(){
document.getElementById(d[ele.value]).checked=true;
}

[#74121] Thursday, November 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrellhunterm

Total Points: 82
Total Questions: 109
Total Answers: 98

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
terrellhunterm questions
Mon, Aug 31, 20, 00:00, 4 Years ago
Mon, Aug 5, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Mon, Mar 11, 19, 00:00, 5 Years ago
;