PDA

View Full Version : default radio checked



game
09-06-2013, 11:18 PM
how can i keep the trained radio checked when the page is load or refreshed?


<script type='text/javascript'>
function show_hide(show, hide)
{
document.getElementById(show).style.display="block";
document.getElementById(hide).style.display="none";
}
</script>

<input type="radio" name="relationship" value="trained" onClick="show_hide('trained', 'tutor')" checked /> Trained <input type="radio" name="relationship" value="tutor" onClick="show_hide('tutor', 'trained')" /> Tutor

<div id="trained" style="padding-top: 10px; display: block;">
I was trained by: <input type="text" class='input_text' id='username1' name="username1" size="30" value="{$user_name1}" tabindex="0" />
</div>


<div id="tutor" style="padding-top: 10px; display: none;">
I was the tutor of: <input type="text" class='input_text' id='username2' name="username2" size="30" value="{$user_name2}" tabindex="0" />
</div>

jscheuer1
09-07-2013, 03:40 AM
<script type='text/javascript'>
function show_hide(show, hide)
{
document.getElementById(show).style.display="block";
document.getElementById(hide).style.display="none";
}
//window.onunload = function(){};
</script>

<input type="radio" name="relationship" value="trained" onClick="show_hide('trained', 'tutor')" checked /> Trained <input type="radio" name="relationship" value="tutor" onClick="show_hide('tutor', 'trained')" /> Tutor

<div id="trained" style="padding-top: 10px; display: block;">
I was trained by: <input type="text" class='input_text' id='username1' name="username1" size="30" value="{$user_name1}" tabindex="0" />
</div>


<div id="tutor" style="padding-top: 10px; display: none;">
I was the tutor of: <input type="text" class='input_text' id='username2' name="username2" size="30" value="{$user_name2}" tabindex="0" />
</div>
<script type="text/javascript">
document.getElementsByName('relationship')[0].checked = true;
</script>



Notice the two highlighted areas. The first is a comment but will be required for some browsers in some cases (like Firefox or Opera, if the page is in standards mode) if you want the code in the second area to execute when navigating to the page via the browser's back or forward buttons. If so and that's what you want, uncomment it (remove the red slashes).

The second highlighted area is what does the work by setting the [0] indexed (the first) radio to checked. Since only one radio in a group of two or more of the same name may be checked, the other will be unchecked if it was checked.

game
09-07-2013, 06:14 AM
thank you that worked

sent you a small donation, again thanks for the help