When is jquery like this, it works:
if( jQuery('.cmp .input-rangeslider-' inputName).hasClass('do-not-update') ){
jQuery('.cmp .input-rangeslider-' inputName).removeClass('do-not-update');
}
But when i write jquery like this:
if(jQuery('.cmp .nemovitosti input[name="type"]:checked')){
$(".answer").show();
}
else{
$(".answer").hide();
}
it cause this error: Uncaught TypeError: $ is not a function
CodePudding user response:
When using Wordpress the $
variable does not refer to jQuery (by default). To work around this you need to alias it yourself in the document.ready event handler.
Also note that your if
statement in the second code example is flawed, as a jQuery object always equates to true
. You need to move the :checked
selector to a separate call to is()
, which will return a boolean:
jQuery($ => {
if ($('.cmp .nemovitosti input[name="type"]').is(':checked')) {
$(".answer").show();
} else {
$(".answer").hide();
}
});
Finally, this logic can be reduced further by providing the boolean value from is()
to the toggle()
function, which will display or hide the target element.
jQuery($ => {
$(".answer").toggle($('.cmp .nemovitosti input[name="type"]').is(':checked'));
});