1

I have dropdown list like

 <select id="ddlProjects">
   <option value="315">resproject</option>
   <option value="320" style-"display:inline">newheavn</option>
   <option value="395" style="display:inline">cealon sales</option>
   <option value="395" style="display:inline">cealon sales</option>
 </select>

Now , I want count of all elements which are "display:inline" on change, how can i get it.

2
  • SideNote: A typo in line style-"display:inline" Commented Jun 23, 2014 at 9:29
  • Might help you jsfiddle.net/9SMZD Commented Jun 23, 2014 at 9:32

4 Answers 4

1

Use attribute selector in jquery to get length of the options in dropdownlist

    $('#ddlProjects').change(function(){
    var len = $(this).find('option[style="display:inline"]').length
    console.log(len)
   });

DEMO

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

Comments

1

By default the display property of <option> is set to inline

If you still need to filter according to this property, use this,

$('#ddlProjects').change(function(){
    var count = $('option',this).filter(function(){
        var css =$(this).attr('style');
        return css !=undefined && css.indexOf('display:inline')>-1;
    }).length;
    console.log(count);
});

Demo

Comments

0

Try to use .filter() at this context,

$('#ddlProjects').change(function(){
  var count = $(this).children().filter(function(){
    return $(this).css('display') === "inline" 
  }).length;
  console.log(count);
})

Try to use attribute contains selector at this situation, since the default display property of an option element is inline.

$('#ddlProjects').change(function(){
    var count = $('option[style*="display:inline"]',this).length;
    console.log(count);
});

DEMO

2 Comments

This won't work as the default display of an option is inline. It will always return the total number of option elements, unless they are hidden.
@RoryMcCrossan, you're right, facing the same problem.
0
$('#ddlProjects option').filter(function() {
    return $(this).css('display') === 'inline';
}).length;

1 Comment

Please try to flesh out your answer a bit more. Just posting a code block without any explanation or context is not good. In its current state your answer might not be of any use to people looking for an answer to this question in the future.

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.