I want the switch button to remember selected option. Because when I refresh browser my selection is resetting.. How can I do that?
Here is my code:
HTML:
<input type="button" value="Tak" onclick="return change(this);" />
JS:
<script>
function change( el )
{
if ( el.value === "Tak" ){
el.value = "Nie";
}else{
el.value = "Tak";
}
}
</script>
Client side values will reset when you refresh the browser.
You need to store in cookie
or session
for that
Include this to simply set and get cookie. Something like:
<head>
<script src="path/to/jquery.js"></script>
<script src="path/to/jquery.cookie.js"></script>
</head>
...
<input type="button" value="Tak" onclick="return setValue(this)" id="btn"/>
Now you can get/set cookie and change input value whit no problem, something like:
function setValue(elem){
if(elem.value == "Tak"){
elem.value = "Nie";
$.cookie("btnState", true);
} else {
elem.value = "Tak";
$.cookie("btnState", false);
}
}
function getValue(){
var c = $.cookie("btnState");
if(c != null){
if(c){
$('#btn').val("Nie");
} else {
$('#btn').val("Tak");
}
} else {
//first page view
$.cookie("btnState", true);
$('#btn').val("Nie");
}
}
trigger getValue()
on page load to check cookie value and set input value
EDIT:
To fire a function on page load use jQuery .ready
function like this:
<script>
$(document).ready(function() {
getValue();
});
</script>
jQuery .ready documentation .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.