4

I have a problem. I'm trying to hide div if text is 0. My code is:

<script type="text/javascript">
    $(function () {
        if ($(".notification-counter").text() == "0") {
            $(".notification-counter").hide();
            $(".notification-container").hide();
        }
    });
</script>

<div class="dropdown nav-search pull-right <?php $this->_c('login') ?>">
    <a href="../pm/" class="dropdown-toggle"><i class="fa fa-inbox"></i>
        <div class="notification-container">
            <div class="notification-counter">
                <?php
                      jimport( 'joomla.application.module.helper' );
                      $module = JModuleHelper::getModule('mod_uddeim_simple_notifier');
                      echo JModuleHelper::renderModule( $module, $attribs );
                ?>
            </div>
        </div>                                  
    </a>    
</div>

but it's not working... anyone can help? thanks for answers!

1
  • 10
    Try if ($.trim($(".notification-counter").text()) === "0")you have a space in DIV. Be aware, .text() will return the text of only first matched element, so if you have more than one .notification-counter element, you have to check it inside a loop and hide only respective container Commented Jan 23, 2015 at 12:29

5 Answers 5

4

Try using parseInt() to make your comparison a number vs. a number rather than comparing text strings (it alleviates issues with whitespace. JSFIDDLE

$(function () {
    if (parseInt($(".notification-counter").text()) == 0) {
        //$(".notification-counter").hide();
        $(".notification-container").hide();
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

as a side note, you don't need to hide the counter, you are already hidding the container. But that's said, this is more readable solution than trimming text imho.
@A.Wolff good point... i commented out the hide counter line.
3

Use trim as there are white spaces

FIDDLE

$(function () {
    if ($.trim($(".notification-counter").text()) == "0") {
        $(".notification-counter").hide();
        $(".notification-container").hide();
    }
});

2 Comments

Why downvotes ? I gave correct answer with fiddle !!
Looks like there's someone that's downvoting everything.. :/
2

Just remove the quotes around 0, and it would work fine.

$(function () {
    if ($(".notification-counter").text() == 0) {
        $(".notification-counter").hide();
        $(".notification-container").hide();
    }
});

Additional Information: Since many here seem to be unclear, here's a little helper: Try this in your console

 //hit F12 to view the console
var counter = $(".notification-counter");
var container = $(".notification-container");
console.log(container.text(), container.html());
console.log(container.text() == 0,container.text() == "0");
//true, false
console.log(typeof 0, typeof "0");
//number, string

JSFiddle

Comments

1
var notificationCounter = $('.notification-counter');
if (notificationCounter.text().trim() === '0') {
  notificationCounter.closest('.notification-container').hide();
}

Comments

1

Try this: instead of .text() put .html()

$(function() {
  if ($(".notification-counter").html() == "0") {
    $(".notification-counter").hide();
    $(".notification-container").hide();
  }
});

1 Comment

Welcome to Stack Overflow. Where possible, it's better to put a bit of explanation of what your code does, as well as linking to relevant documentation -- this will help to gain reputation and privileges! For more tips, take a look at How to Answer. Thanks!

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.