I have tried everything and nothing works. I have this checkbox
<input type="checkbox" onchange="WriteFormSelections('SearchForm', 'AccommodationType', 'AccommodationTypeSelections', 'All', 'AccommodationType-0', false, '_', true);" value="Hotel" name="AccommodationType_Hotel" id="AccommodationType-2">Hotel
And I want to check it when the page loads. I have tried lots of things. One of the things I can’t work out is, am I supposed to use the id or the name of the checkbox to check it?
Here’s my current code. I have tried to use prop as suggested by this post but that doesn’t work either.
<script type="text/javascript">
$('AccommodationType-2').attr('checked', true);
</script>
You are missing a # in front of your selector.
$('AccommodationType-2').attr('checked', true);becomes
$('#AccommodationType-2').attr('checked', true);…depending on the version of jQuery you’re using I’d recommend looking at
.prop()too, which is to be used for property access on objects.Additionally, as the other answerers have rightly pointed out – you do need to wait for the DOM to be ready by wrapping a
document.ready()around your checkbox-checking line.