galvin Posted March 9, 2011 Share Posted March 9, 2011 I have several sets of radio buttons, all in the same general format as this... <div id="radio1"> <input type='radio' name='name' id='radioci1' value='yes' onclick="reveal(????)" ><label for='radioci1' class='yes'>Yes</label> <input type='radio' name='name' id='radioci2' value='no' onclick="hide(????)" ><label for='radioci2' class='no'>No</label> </div> </div> <div id="dynamic1" ><input type="text" name="textfield" value="" /></div> I have this javascript to show/hide the "dynamic" field when either radio button is clicked. I can make this work by typing the exact ID ("dynamic1"in the parentheses, but I have a BUNCH of sets of radio buttons, so I'd rather not have to write several sets of javascript. In other words, i will also have a "dynamic2" field. I gotta think there is way to use this simple script below and have it apply to whichever set of radio buttons is being used. Maybe something using "this"? Wherever I put "????" in the code above and below is where I'm not sure what to put in there, if anything. Can anyone help me out? function reveal(????) { document.getElementById(????).style.display="block"; } function hide(????) { document.getElementById(????).style.display="none"; } Quote Link to comment https://forums.phpfreaks.com/topic/230054-use-simple-javascript-for-multiple-element-ids/ Share on other sites More sharing options...
nogray Posted March 9, 2011 Share Posted March 9, 2011 You can alter the id values for the divs to include the input name as a substring and use the this.id in the input click event. e.g. <div id="radio1"> <input type='radio' name='name' id='radioci1' value='yes' onclick="reveal(this.name)" ><label for='radioci1' class='yes'>Yes</label> <input type='radio' name='name' id='radioci2' value='no' onclick="hide(this.name)" ><label for='radioci2' class='no'>No</label> </div> <div id="dynamic_name" ><input type="text" name="textfield" value="" /></div> function reveal(id) { document.getElementById('dynamic_'+id).style.display="block"; }..... Quote Link to comment https://forums.phpfreaks.com/topic/230054-use-simple-javascript-for-multiple-element-ids/#findComment-1184890 Share on other sites More sharing options...
galvin Posted March 9, 2011 Author Share Posted March 9, 2011 That will do what I need, thank you very much!!! Quote Link to comment https://forums.phpfreaks.com/topic/230054-use-simple-javascript-for-multiple-element-ids/#findComment-1185012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.