Based on the below markup, how (in JQuery) would i select the options that start with Urgent- or Urgent - ?
<select name="List1" id="list1">
<option>One</option>
<option>Two</option>
<option>urgent-Three</option>
<option>Urgent-Four</option>
<option>Urgent - Five</option>
</select>
Based on the answer below I am using the following JQuery to wrap the matched elements in an <optgroup>.
$opts = $("#list1 > option").filter(function() {
return /^Urgent\s?-/.test(this.innerHTML);
});
$opts.wrapAll('<optgroup label="Urgent">');
I need to remove the matched text from the option text resulting in markup like this:-
<select name="List1" id="list1">
<option>One</option>
<option>Two</option>
<optgroup label="Urgent">
<option>Three</option>
<option>Four</option>
<option>Five</option>
</optgroup>
</select>
You can use
filtermethod and regular expressions:DEMO: http://jsfiddle.net/th83R/
UPDATE. To follow your updated question, you have to modify the code a bit.
DEMO: http://jsfiddle.net/th83R/3/