2

I must be doing something wrong here. I want jQuery to check if the width of a CSS element is greater than 0 then add CSS property:

<script>
$(document).ready(function(){
    $(".neo-P4EMilestonesMilestone").each(function() {
        $(this).css('width') !=0) {
            $(this).css("display", "block");
        });
    });
});
</script>

The class element has a default width of 0:

<div class="neo-P4EMilestonesMilestone" style="width:0">

I want jQuery to look into any .neo-P4EMilestonesMilestone classes and if the width IS NOT set to "0" then add "display:block". Like this:

<div class="neo-P4EMilestonesMilestone" style="width:10%;display:block;">
3
  • What's the question/problem? Commented Mar 18, 2015 at 19:29
  • I want jQuery to add "display:block;" if the width is not "0". Commented Mar 18, 2015 at 19:32
  • I think for starters there's a syntax problem. Just before the innermost line, you have a curly brace but no "if", "function", "for", or anything else that would allow a block-separator. Commented Mar 18, 2015 at 20:14

2 Answers 2

3

You could simplify it to the following:

Example Here

$('.neo-P4EMilestonesMilestone').each(function () {
    $(this).css('display', $(this).width() > 0 ? 'block' : '');
});

Your code snippet wasn't working for a couple reasons. For one, there were syntax errors. In addition, when you retrieve the element's width via $(this).css('width'), the px unit is included. You would have had to parse the string parseInt($(this).css('width'), 10) in order to check if it is greater than 0. Instead of doing that, you can just retrieve the width using $(this).width(), and then that isn't necessary.

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

Comments

0

Yuu have a few syntax errors. No if keyword and no closing braces. See below.

$(".neo-P4EMilestonesMilestone").each(function () {
    if ($(this).width() != 0) {
        $(this).css({
            'display': 'block',
                'width': '10 % '
        });
    }
});

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.