jimi
jimi

Reputation: 213

How to add css based on another value?

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

Answers (7)

Jai
Jai

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

Surya Peddada
Surya Peddada

Reputation: 265

You can do it like so:

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

Upvotes: 0

adeneo
adeneo

Reputation: 318232

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

FIDDLE

Upvotes: 0

David Hedlund
David Hedlund

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

connectedsoftware
connectedsoftware

Reputation: 7087

Try the following syntax:

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

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Try

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

Upvotes: 0

gustavohenke
gustavohenke

Reputation: 41440

You can do it like so:

(function( $ ) {
  $(document).ready(function () {
    if ( $('.class1').val() !== "1" ) {
      $('.class2').hide();
    }
  });
 })( jQuery );

Upvotes: 2

Related Questions