1

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...

7 Answers 7

2

You can do it like so:

(function( $ ) {
  $(document).ready(function () {
    if ( $('.class1').val() !== "1" ) {
      $('.class2').hide();
    }
  });
 })( jQuery );
Sign up to request clarification or add additional context in comments.

Comments

0

Try

jQuery(function ($) {
    if ($('.class1').val() != '1') {
        $('.class2').css("display","none");;
    };
});

Comments

0

Try the following syntax:

  $('.class1').val() != '1'

Comments

0

$('.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.

Comments

0
$('.class2').toggle( $('.class1').val().trim() != '1' );

FIDDLE

Comments

0

You can do it like so:

$(document).ready(function () {
  if ( $('.class1').val() != "1" ) {
    $('.class2').attr('style','display:none;');
  }
});

Comments

0

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.