Results 1 to 3 of 3

Thread: default radio checked

  1. #1
    Join Date
    Jun 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default default radio checked

    how can i keep the trained radio checked when the page is load or refreshed?

    HTML Code:
    <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>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    thank you that worked

    sent you a small donation, again thanks for the help

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2013, 09:52 PM
  2. If Else Input checked?
    By mikster in forum PHP
    Replies: 4
    Last Post: 08-31-2011, 02:44 AM
  3. Default OnClick for Radio Buttons Not Cancelling
    By Torin Mai in forum JavaScript
    Replies: 2
    Last Post: 06-04-2009, 03:26 AM
  4. Replies: 1
    Last Post: 01-23-2008, 08:32 AM
  5. If radio button is checked show layer
    By sanjay_es in forum JavaScript
    Replies: 0
    Last Post: 11-14-2005, 05:52 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •