0

I have a function that check if CSS value exists, the problem is that I need the function to work only when the CSS class exists, currently the function running all the time because I'm using else condition (that need to re do the if condition).

 //use for @media only screen and (max-width: 767px) detection    
 $(window).resize(function(){
    if ($('#mobile-view').css('visibility') === 'hidden') {
        $("#product-gallery").insertAfter("#product-info");
    }
     else { 
         $("#product-info").insertAfter("#product-gallery");
    }
 });  
0

4 Answers 4

3

You could use the :hidden pseudo selector:

if ($('#mobile-view').is(':hidden')) {
    $("#product-gallery").insertAfter("#product-info");
} else {
    $("#product-info").insertAfter("#product-gallery");
}

See also: .is() and :hidden

Sign up to request clarification or add additional context in comments.

Comments

1

Change it to

if ($('#mobile-view').is(":visible")) {

    $("#product-info").insertAfter("#product-gallery");
} else {
     $("#product-gallery").insertAfter("#product-info");
 }

7 Comments

isn't it right if visible insert after product gallery or else insert after product-info?
it seems that the OP has posted the inverse condition : if #mobile-view is invisible then we insert the product gallery...
he say this if ($('#mobile-view').css('visibility') === 'hidden') { $("#product-gallery").insertAfter("#product-info"); } that means if hidden then insert after product info ? Correct?
Yes correct, but your logic is if visible, then insert after product info... :)
No :) if Visible do this $("#product-info").insertAfter("#product-gallery"); that is insert after product gallery.
|
0

you can use hasClass

if ($('#mobile-view').hasClass(className)) {
 // put your logic inside this.
}

Comments

0

Try using $("#mobile-view").is(":visible") == true

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.