Home › Forums › JavaScript › Checkbox won't 'check'
- This topic is empty.
-
AuthorPosts
-
July 16, 2015 at 4:13 am #205149
CameronDuthie
ParticipantHi there,
I’m trying to implement a simple fade in/fade out content block which is controlled by a checkbox.
I’ve managed to set everything up ok but for some reason when you click on the checkbox it won’t ‘check’.
I’ve set up a fiddle here > https://jsfiddle.net/dfdhssq8/
Would anyone know how to force the ‘check’ state when clicked?
Many thanks,
July 16, 2015 at 4:24 am #205150Paulie_D
MemberIf you disable the JQ it does check so there’s that.
What I suspect is happening is the the the click event is overriding the ‘checking’ function and disabling it.
EDIT – Yep.
A quick google search found this SO question – http://stackoverflow.com/questions/8423217/jquery-checkbox-checked-state-changed-event
$(".checkbox").change(function() {
if(this.checked) {
//Do stuff
}
});July 16, 2015 at 4:59 am #205151CameronDuthie
ParticipantThanks @Paulie_D – much appreciated!
That seems to have done the trick, except when i uncheck it doesn’t fade back – only when it’s checked again does it fade.
https://jsfiddle.net/dfdhssq8/1/
Any ideas?
July 16, 2015 at 5:17 am #205152Paulie_D
MemberFirst attempt (my JQ is poor)
July 16, 2015 at 8:38 am #205172CameronDuthie
Participant@Paulie_D – Great stuff! That seemed to sort it out.
Can’t be that poor, it’s a damned sight better than mine! :)
Thanks again sir!
August 31, 2017 at 11:07 am #259640jcpux
ParticipantI know this is an old thread… but here’s a pure CSS solution https://jsfiddle.net/20obhbok/
September 26, 2019 at 12:04 am #296458larabrian
ParticipantAlso, jQuery is() method is to check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments. If no element fits, or the selector is not valid, then the response will be ‘false’. This method traverses along the DOM elements to find a match, which satisfies the passed parameter.
if ($(“input[type=checkbox]”).is(“:checked”)) {
alert(“Checked”);
}October 2, 2019 at 1:52 pm #296818nicholp63
ParticipantI know this is an old thread
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.