Set attribute disabled to options NOT containing a certain string

Posted by kuswantin on Stack Overflow See other posts from Stack Overflow or by kuswantin
Published on 2010-05-12T19:47:22Z Indexed on 2010/05/12 20:04 UTC
Read the original article Hit count: 116

Filed under:
|

I know this should be easier if I could only use optgroups. But I am trying to avoid hacking core of my CMS, so jQuery should come to rescue again, hopefully.

I have a select with options in a hierarchy sometimes, and I want to put attributes disabled to any of options containing text NOT starting with a dash. I want to code like:

Set disabled attributes to selectors with options text NOT starting with a dash ("-").

<select id="options">
<option value="" selected="selected">- Please choose -</option>
<option value="1">Parent1</option>
<option value="2">-child1</option>
<option value="3">-child2</option>
<option value="4">-child3</option>
<option value="5">-child4</option>
<option value="6">Parent2</option>
<option value="7">-child5</option>
<option value="8">-child6</option>
<option value="9">-child7</option>
<option value="10">-child8</option>
</select>

The closest solution is here http://stackoverflow.com/questions/2012299/contain-start-by,

$.extend($.expr[':'], {
    startsWith: function(elem,match) {  
        return (elem.textContent || elem.innerText || "").indexOf(match[3]) == 0;
    }  
});

But I can't seem to do it right this far.

Any help would be very much appreciated. Thank you very much.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about attributes