this is my checkbox
<?php $create = array(
'name'=> 'single_obs_value',
'id' => 'Heart_Rate',
'class' => 'singleobs',
'value' => $cp->odvalue_list_id,
'checked' => sel_checkbox($singleobsnormal['single_obs_value'] == $cp->odvalue_list_id),
'style'=> 'float: left; margin-right: 10px;'
);
?>
When my checkbox is ticked it shows a hidden span and when unticked it hides the span. I have done this using javascript.
$('#Heart_Rate').on("change", function() {
if (this.checked) {
$('#hrsObs').fadeIn('fast');
} else {
$('#hrsObs').fadeOut('fast');
}
}
This function works perfectly for if i am checking or unchecking the box. But when the page loads i need a function to check if the checkbox was checked or not and thus hide or show the span which has been given a class of #hrsObs. Any suggestions?
$('#Heart_Rate').attr('checked') // give you you the information
You can use this in DOM ready event:
$(document).ready(function(){
if($('#Heart_Rate:checked').length){
$('#hrsObs').fadeIn('fast');
}});
Give this a try:
jQuery(document).ready(function(){
//cache these since you'll be using them a few times
var $heartRate = $('#Heart_Rate'),
$hrsObs = $('#hrsObs');
$heartRate.on("change", isChecked());
function isChecked(){
if($heartRate.prop("checked")){
$hrsObs.fadeIn('fast');
}
else{
$hrsObs.fadeOut('fast');
}
}
//run function now that page has loaded
isChecked();
});
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.