1

In my following Select dropdown, I wanna hide the options which have only number in them (or which start with number).

As an example I wanna hide 28(2), 29(2), 35(2) and 49(2).

<select data-facet="field_video_tags" id="ajax-facets-selectbox-field-video-tags" name="field_video_tags" class="form-select hide processed"> <option value="0">Select all</option> <option value="5">First Option (11) </option> <option value="38">Second Option (9) </option> <option value="13">Third Option (3) </option> <option value="28">28 (2) </option> <option value="29">29 (2) </option> <option value="35">35 (2) </option> <option value="49">49 (2) </option> <option value="8">Forth Option (2) </option> </select> 

This is what I tried:-

$('select#ajax-facets-selectbox-field-video-tags').filter(function() { return ($.isNumeric($(this).val())); }).addClass('hide'); 

Ofcourse, .hide is defined as {display:none} in my css.

Thanks

1
  • Why do you have .hide in the class of the select option? you are hiding the entire select tag Commented Jul 30, 2014 at 21:40

3 Answers 3

3

Here's what I would do:

http://codepen.io/anon/pen/yjBfl

$('#ajax-facets-selectbox-field-video-tags option') .filter(function () { return !isNaN(parseInt($(this).text())); }) .hide(); 

enter image description here

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

Comments

0

You're somehow on the right track: DEMO

$('#ajax-facets-selectbox-field-video-tags option').filter(function() { return /^\d+/.test( $(this).text() ); }).hide(); 

1 Comment

You forgot the # in your selector.
-1

The behavior of applying display: none (which is what jQuery's hide() does) to <option>s is inconsistent between clients. So you should not rely on that and remove the elements instead.

$('#ajax-facets-selectbox-field-video-tags option').each(function() { var $option = $(this); if(null !== $option.text().match(/^\d/)) { $option.remove(); } }) 

Here's a JSFiddle.

Edit: you probably can hide the items. But I still wouldn't recommend it. Feels like hide()ing a <source> element in an <video>.

7 Comments

Really weird. In my example they don't: jsfiddle.net/tSGX4/1. Still, if they do, seems a bit "hackey" to me. <option>s are data sources for the <select> element, not visual elements.
In your example ... they are hidden for me.
Chrome 37. They don't for me. Anyway, just have a look at the edit in my post and reconsider your downvote if you agree. :) Thanks!
Hmmm ... I was using Chrome Version 36.0.1985.125 m. That said, if you remove the you cannot hide options statement I'll remove my DV. Obviously you can hide them.
Updated my answer. Fair enough?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.