hi i am javascript newbie.
I have checkbox that slideDown other input , i want to keep that checkbox checked and the other input showed after refreshing the page if the input checkbox checked
javascript:
function validateSitec(){
if (document.getElementById('sitecheck').checked){
$('#sitecheck').prop('checked', true);
$('#closedmsg').slideDown();
}else{
$('#closedmsg').slideUp();
$("#sitecheck").removeProp("checked").checkboxradio("refresh");
}
}
this my inputs:
<label for="sitecheck">
<span style="font-weight:bold;">close site+ msg:</span>
</label>
<input name="" type="checkbox" id="sitecheck" onclick="validateSitec()" /><span style="font-weight:bold;">click to activate msg</span><br>
<input type="text" name="closedmsg" id="closedmsg" style="width:440px;height:120px;display:none;" value="enter closed msg.."/>
i want if checked stay checked.. and wont change after refreshing the page , then when unchecked so back to normal and be unchecked when refreshing the page..
should i use php for making it not change after checking with javascript?
Edited:
Thanks to all for helping
credit goes to : leiyonglin .
The working code for anyone who like to use it:
download first:
https://github.com/carhartl/jquery-cookie
then use this codes working awesome :
JavaScript:
<script type="text/javascript">
function validateSitec(){
if (document.getElementById('sitecheck').checked){
$('#sitecheck').prop('checked', true);
$('#closedmsg').slideDown();
$.cookie("cookieChecked", "#sitecheck");
}else{
$('#closedmsg').slideUp();
$("#sitecheck").removeProp("checked");
$.cookie("cookieChecked","");
}
}
$(function(){
var cookieChecked = $.cookie("cookieChecked");
if(cookieChecked){
$('#sitecheck').prop('checked', true);
$('#closedmsg').show();
}else{
$("#sitecheck").removeProp("checked");
$('#closedmsg').hide();
}
})
</script>
html inputs:
<label for="sitecheck">
<span style="font-weight:bold;">close site temp:</span>
</label>
<input name="" type="checkbox" id="sitecheck" onclick="validateSitec()" /><span style="font-weight:bold;">close site and add message</span><br>
<input type="text" name="closedmsg" id="closedmsg" style="width:440px;height:120px;display:none;" value="<?php echo $data['csitemsg']; ?>" />
This working perfect thx again all.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…