Reputation: 213
i want to hide a class
when a value is not = 1
(function ($) {
$(document).ready(function () {
if (!$('.class1').value='1') {
$('.class2').css("display","none");;
});
});
}(jQuery));
but it is not working...
Upvotes: 1
Views: 72
Reputation: 74738
Your issue is single =
equal here:
if (!$('.class1').value='1')
you can change to ==
or ===
:
if (!$('.class1').value === '1')
or this way:
if ($('.class1').value !== '1')
Upvotes: 0
Reputation: 265
You can do it like so:
$(document).ready(function () {
if ( $('.class1').val() != "1" ) {
$('.class2').attr('style','display:none;');
}
});
Upvotes: 0
Reputation: 129792
$('.class1')
will yield a reference to a jQuery object wrapping zero or more elements. It will not be a reference to a DOMNode. You do not at that point have access to any property named value
. There is a function called val
that will yield the value of the first element matched by the selector, if any.
if($('.class1').val() != '1') {
$('.class2').hide();
}
Furthermore, you're trying to use =
to check for equality, but =
is only used for assignment. You should use ==
in conditions. Now that you're looking for the inverse of equality, you shouldn't use !X==Y
but X!=Y
.
Upvotes: 0
Reputation: 388316
Try
jQuery(function ($) {
if ($('.class1').val() != '1') {
$('.class2').css("display","none");;
};
});
Upvotes: 0
Reputation: 41440
You can do it like so:
(function( $ ) {
$(document).ready(function () {
if ( $('.class1').val() !== "1" ) {
$('.class2').hide();
}
});
})( jQuery );
Upvotes: 2