2

I am looking for a way to determine with JavaScript / jQuery if the WooCommerce store notice is displayed. The HTML for the store notice looks like this...

<p class="woocommerce-store-notice demo_store" style="display: block;">
    This is a store notice
</p>

I have tried to do this using the following...

jQuery(document).ready(function(){
    if ( jQuery('.woocommerce-store-notice').css('display') == 'none') {
        console.log('Store Notice Hidden');
    } else {
        console.log('Store Notice Visible');
    }
});

But this is telling me that the notice is hidden everytime, even when it is visible.

Could this be something to do with how the store notice get's displayed? Maybe it get's set after the dom has loaded?

4
  • well, when or in which case does this notification get shown ? Commented Aug 14, 2018 at 12:04
  • I tried it on jsfiddle and it works fine. Maybe the problem is where the jquery script is located in the code. Commented Aug 14, 2018 at 12:05
  • The notification is displayed on page load, but it's display value seems to be set inline using Javascript. Going to try and work out where it gets set now Commented Aug 14, 2018 at 12:06
  • that's the key. to find out when it is shown and you can use that event. But you could try to use window.on('load') instead of ready Commented Aug 14, 2018 at 12:07

3 Answers 3

2

The element is removed when the store notice is disabled. The CSS display property is not available hence undefined. Try the below code

jQuery(document).ready(function(){
    if ( jQuery('.woocommerce-store-notice').css('display') == undefined) {
        console.log('Store Notice Hidden');
    } else {
        console.log('Store Notice Visible');
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Welcome back hemnath :)
@LoicTheAztec That's insane that you still remember me :) BTW you're grindin on woocommerce topics
0

Check with

if(jQuery('.woocommerce-store-notice').is(':visible')){

}

else{

}

Comments

0

you can find what you are looking for here:

http://api.jquery.com/visible-selector/

Btw => JQuery: if div is visible

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.