Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

refresh page after checkbox is unticked

Status
Not open for further replies.

simonWMC2

Programmer
Aug 5, 2004
161
GB
Hello...

I have created a page in dreamweaver with a checkbox on it. When the user ticks the box a function is run. However, if they then change their mind and untick the box, the function is still run... How can i force the page to refresh if the user unticks the box ?

cheers
 
Do you really want to refresh the page or just prevent the function from running if the box is unchecked?

If you are using an onclick event for the checkbox then you just check the state of the box prior to executing the rest of the code.
Example:
Code:
function boxCheck() {
  if (document.myForm.myCheckBox.checked) {
    //Box has been checked so execute your function
  }
  else {
    //Box was UN-checked so do not execute function or run some other function instead.
  }
}

How you do the test depends on how you are responding to the click but this should give you an idea.

If you want to refresh the page then you have to consider if you are OK with losing all the form data or if you need to preserve it. Some browsers will keep the data as the page refreshes and some will not. If you want to save the data then you will probably want to submit the page to itself and read back the posted values to populate the page with.


Stamp out, eliminate and abolish redundancy!
 
thanks. Yes i really need to refresh the page
 
Since niteowl gave an example of how to check the checkbox condition, then all you need left is code to refresh the page. This should work fine:

Code:
document.location = document.location;

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
sorry to be a pain but is it possible to just make the page refresh if the check box is clicked twice ? Ie, checked then unchecked...



 
Building on the code above, try this:

Code:
function boxCheck() {
	if (document.myForm.myCheckBox.checked) {
		//Box has been checked so execute your function
	} else {
		location.reload(true);
	}
}

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You can try something like this.
When it get's clicked it tests if the form is already checked and if so refreshes the page and if not it runs whatever function you run when they first check it.
I have not tested this but give it a try.

onclick="if (this.checked) document.location = document.location; else someotherfunction();"

Stamp out, eliminate and abolish redundancy!
 
apologies - i don't think i have explained myself very well.

the problem is that the box is unticked to start with, and the function will run if it is not ticked. When it is ticked the function runs bit with an additional condition, if the box is then unticked it still uses the additional condition. I don't want it to use the additional function but go back to how it was. The best way i could think of doing this was to refresh the page

 
You have to show your code before we can give you specific information on how to modify it.

Stamp out, eliminate and abolish redundancy!
 
Code:
<script type="text/javascript">
    // convert the number of days into a number
    var days = parseInt(_data,10);
    // some simple error checking of data
    if (days > 366 || days == 366 && !_leap) return('too many days');
    if (days < 1) return('too few days');

    // update the number of days for february if it is a leap year
    if (_leap) dayCount[1] = 29;

    var mm = temp = 0;
    if (days <= 366) {
        while (temp < days) temp += dayCount[mm++];
        temp = temp - dayCount[mm-1];
        dd = days - temp;
        return(dd + '/' + mm);
    }
}
function alertDate() {
    var _days = document.getElementById('dayinput').value;
    var _leap = document.getElementById('leap').checked;
	//put into text field
	document.getElementById('answerly').value = getDateFromJulian(_days,_leap);
    //alert(getDateFromJulian(_days,_leap));
}
//function MM_openBrWindow(theURL,winName,features) { //v2.0
  //window.open(theURL,winName,features);
//}

</script>

cheers
 
The following html will create a document w/one checkbox. When you check the box it will open an alert message: "I'm Working!"; when you un-check the box it will reload the page.

Code:
<html>
<head>
<script language=JavaScript>
<!--
 function RegisterClick(item)
 {
  if(item.checked == true)
  {
   alert("I'm working!");
  }else{
   window.location.reload();
  }
 };
//-->
</script>
</head>
<body>
<form name=blah>
<input type=checkbox name=foo onClick="RegisterClick(document.blah.foo);">
</input>
</form>
</body>
</html>




I hope this helps;
Rob Hercules
 
Where are you getting [tt]_data[/tt] from in your script?
Could the first half of your script (everything before the [tt]alertdate()[/tt] function) be enclosed in another function, then called when the form is submitted? If so, I think this shoudl fix it:
Code:
<script type="text/javascript">
function calculateDate(_data);
{
    var _leap = document.getElementById('leap').checked;
    // convert the number of days into a number
    var days = parseInt(_data,10);
    // some simple error checking of data
    if (days > 366 || days == 366 && !_leap) return('too many days');
    if (days < 1) return('too few days');

    // update the number of days for february if it is a leap year
    if (_leap) dayCount[1] = 29;

    var mm = temp = 0;
    while (temp < days) temp += dayCount[mm++]
    {
        temp = temp - dayCount[mm-1];
        dd = days - temp;
        return(dd + '/' + mm);
    };
};
function alertDate() {
    var _days = document.getElementById('dayinput').value;
    var _leap = document.getElementById('leap').checked;
    //put into text field;
    document.getElementById('answerly').value = getDateFromJulian(_days,_leap);
    //alert(getDateFromJulian(_days,_leap));
};
//function MM_openBrWindow(theURL,winName,features) { //v2.0
  //window.open(theURL,winName,features);
//};

</script>




I hope this helps;
Rob Hercules
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top