java - Radio buttons and text box event handling -
i'm making page in jsp , unable determine how following.
i have bunch of radio buttons generated dynamically in loop
<% (something : somethings) { (random random : something.getrandoms ()) { %> <input type ="radio" name="<%= something%>" value="<%= random.tostring()%>"><%= random%> <br> <% }%> text here : <input type = "text" name="<%= something%>text" placeholder="some more text here"/> <% }%> what want is:
when 1 of radio buttons checked, text box should made empty , when text entered in text box, radio buttons should cleared.
i tried in javascript saying <name of text box>.value="", doesn't work.
as discussed in comments, answer assumes have wrapped fieldset around each grouping of radio buttons , text box:
var sets = document.getelementsbytagname('fieldset'), setscount = sets.length, clearbox = function (box) { box.value = ''; }, clearfields = function (fieldset) { var buttons = fieldset.queryselectorall('input[type="radio"]'), buttoncount = buttons.length, textbox = fieldset.queryselector('input[type="text"]'), j; (j = 0; j < buttoncount; j += 1) { buttons[j].onclick = function () { clearbox(textbox); }; } textbox.onkeypress = function () { (j = 0; j < buttoncount; j += 1) { buttons[j].checked = false; } }; }, i; (i = 0; < setscount; += 1) { clearfields(sets[i]); }
Comments
Post a Comment