Reputation: 141
So, quick question - I check to see if a checkbox is clicked on an html doc and then when the user clicks on a continue button, it takes them to the next page, which will do stuff based on whether or not that checkbox was checked. What's the easiest way of doing this?
In the index.html file (homepage) I have
if ($("#checkArray").is(":checked")) {
console.log("permission granted");
} else {
console.log("No permissions granted");
}
window.location='/auth/facebook';
which checks to see if the check box is marked. it then goes to the next html document (confirmation.html) for authorization of facebook here, I want to call a function if the checkbox was checked (true).
Please help! Thanks!!
Upvotes: 1
Views: 79
Reputation: 1
Following Felix Kling
Try
at index.html
$("input[type='button']").on("click", function(e) {
var checked = $("input[type='checkbox']").prop("checked");
if (checked) {
// do stuff
// if `_checked` === true
console.log(checked);
}
else {
// do stuff
// if `_checked` === false
console.log(checked)
};
var conf = window.location.href;
window.location.href = conf + "?checked=" + checked;
});
at confirmation.html
$(window).on("load", function(e) {
var _checked = (e.target.location.href
.split("?").slice(1,2)[0].split("=")[1] === "true"
? true
: false
);
if(_checked) {
// do stuff
// if `_checked` === true
console.log(_checked);
} else {
// do stuff
// if `_checked` === false
};
});
jsfiddle http://jsfiddle.net/guest271314/j9t598ab/
Upvotes: 0
Reputation: 1705
You can use cookies
or sessionStorage
.
Example with SessionStorage:
if ($("#checkArray").is(":checked")) {
window.sessionStorage.setItem('checked', true);
} else {
window.sessionStorage.setItem('checked', false);
}
window.location='/auth/facebook';
And on other page:
var checked = window.sessionStorage.getItem('checked');
SessionStorage works with modern browsers so if you need to support old browsers like IE8, you should use cookies. You can read more about cookies here: http://www.w3schools.com/js/js_cookies.asp.
Upvotes: 2