SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can radio buttons be empty after refreshing a page

    I have two radio buttons in my form. Now what I found out is that in firefox, if you refresh the form, is a radio button was selected before the refresh, then after the refresh the radio button is still selected. I want to know is there a way by using javascript code to be able to say no radio buttons should be already selected when page reloads or refreshes or does it all depend on browser?

    I tried including in the window.onload=function()
    Code:
    btnRadioO[0].checked==false && btnRadioO[1].checked==false
    which I thought would do the trick but it didn't work. Any ideas?

    Below is code for radio buttons in javascript:

    Code:
    window.onload=function(){
    
    var btnRadioO = document.getElementsByName("weightChoice"); 
    
    btnRadioO[0].checked==false && btnRadioO[1].checked==false;
    }
    Html code:

    Code:
       <table>
                    <tr>
                    <th>6: Provide a Total Weight for your Yourself</th>
                    <td><input type="radio" name="weightChoice" value="yes" onClick="getWeight()"/> Yes</td>
         			<td><input type="radio" name="weightChoice" value="No" onClick="getWeight()"/> No</td>
         			</tr>
         			</table>

  2. #2
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In firefox when you refresh the form, any input is saved and not cleared. You can do something like this on jquery

    $(document).ready(function(){
    $('input[type=checkbox]').attr('checked',false);
    })

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you that worked perfect when I changed input to radio. Thank you for your help

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no need for jquery at all for something so basic as unchecking radio buttons and if you look at the "back end" javascript that jquery runs to perform the same task you'll probably find jquery runs a lot more code.

  5. #5
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webdev1958 View Post
    There is no need for jquery at all for something so basic as unchecking radio buttons and if you look at the "back end" javascript that jquery runs to perform the same task you'll probably find jquery runs a lot more code.
    Yeah, I am sure the javascript code will be much more suitable for this task. But unfortunately I am not really familiar with Javascript OOP. If you could share the code how to do it in jquery, it would be awesome.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,787
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Just place the following at the very bottom of the JavaScript attached to the bottom of the web page and it will do the job for you without needing the 40k+ jQeury library.

    Code:
    var inp = document.getElementsByTagName('input');
    for (var i = inp.length-1; i>=0; i--) {
    if ('radio'===inp[i].type) inp[i].checked = false;
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •