Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript hide DIV tag if text is 0

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!

like image 613
Erick Avatar asked Dec 19 '22 07:12

Erick


2 Answers

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();
    }
});
like image 108
JRulle Avatar answered Dec 31 '22 16:12

JRulle


Use trim as there are white spaces

FIDDLE

$(function () {
    if ($.trim($(".notification-counter").text()) == "0") {
        $(".notification-counter").hide();
        $(".notification-container").hide();
    }
});
like image 43
Pratik Avatar answered Dec 31 '22 17:12

Pratik