www.webdeveloper.com
Results 1 to 6 of 6

Thread: Checkbox Reload

  1. #1
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    28

    Checkbox Reload

    I want to be able to click on a checkbox (or a radio button) and have it reload the page, but also keep the item selected. The code I currently have reloads the page but does not check the checkbox or select the radio button. The code is:

    <form name="myform" method="post" action="thispage.htm">
    <input type="checkbox" name="checkbox" value="checkbox" onClick="javascript:window.location.reload()">
    </form>

    Can anyone hep me out.

  2. #2
    Join Date
    Mar 2005
    Location
    Gilbert, AZ
    Posts
    293
    Try this:
    Code:
    <form name="myform" method="post" action="thispage.htm">
    <input type="checkbox" name="cb" value="checkbox" onClick="reload();">
    </form>
    <script type="text/javascript">
    function reload()
    {
    	if (!document.location.href.match(/checkit/))
    	{
    		var parameter = (document.location.href.match(/\?/) ? '&' : '?') + 'checkit';
    		document.location.href = document.location.href + parameter;
    	}
    	else
    		document.location.reload();
    }
    
    window.onload = function()
    {
    	if (document.location.href.match(/checkit/))
    		document.myform.cb.checked = true;
    }
    </script>
    If you want the user to be able to uncheck the box use this:
    Code:
    <input type="checkbox" name="cb" value="checkbox" onClick="if (this.checked) reload();">

  3. #3
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    28
    This works well. Thanks. Is there an easy way to modify it so that the same thing happens when you uncheck it - that is it stays unchecked and page reloads.

    Cheers,

    Steve

  4. #4
    Join Date
    Mar 2005
    Location
    Gilbert, AZ
    Posts
    293
    This should do what you want:
    Code:
    function reload(cb)
    {
        var url = document.location.href.replace(/[?&]checkit/,"");
    
        if (cb.checked)
            url += (document.location.href.match(/\?/) ? '&' : '?') + 'checkit';
            
        document.location.href = url;
    }
    But I'm really starting to wonder if you don't just want to do document.myform.reset() instead of reloading the whole page. Firefox and IE exhibit different behaviors when you reload a page. IE resets the form while Firefox retains the values. If you do a form.reset() the fields reset to their initial state in either browser.

  5. #5
    Join Date
    May 2011
    Posts
    4
    How do I have value of checkbox on page level?

    Thanks so much for the response.

  6. #6
    Join Date
    May 2011
    Posts
    4
    How do I have value of checkbox (whether it's checked or unchecked) after page reload?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles