javascript - Copy value of Radio Button Selectio to text box -
this question has answer here:
- how selected radio button value using js 15 answers
i trying copy value selected radio button display in text box. however, can not work. have copied code below.
is able me please?
function sync() { var n4 = document.getelementbyid('n4').checked; var n1 = document.getelementbyid('n1'); n1.value = n4.value; }
<form action="" id="n4" onkeyup="sync()"> <input type="radio" name="gender" value="male" id"4" onkeyup="sync()"> male<br> <input type="radio" name="gender" value="female" onkeyup="sync()"> female<br> <input type="radio" name="gender" value="other" onkeyup="sync()"> other </form> <input type="text" name="n1" id="n1" >
i have changed bit of code. first of used onchange event. after ive created array of radio buttons same class name. everytime radio button changes loop , return checked value in input field.
function sync() { var radios = document.getelementsbyname('gender'); for(var = 0; < radios.length; i++){ if(radios[i].checked){ n1.value = radios[i].value; }; }; };
<form action="" id="n4" onkeyup="sync()"> <input type="radio" name="gender" value="male" id"4" onchange="sync()"> male<br> <input type="radio" name="gender" value="female" onchange="sync()"> female<br> <input type="radio" name="gender" value="other" onchange="sync()"> other </form> <input type="text" name="n1" id="n1" >
Comments
Post a Comment