I have the following drop down menu:
<select id="target">
<option value="0">Zero ($123.45)</option>
<option value="1">One ($99.99)</option>
<option value="2">Two ($4.50)</option>
</select>
Using jQuery, how can I select the contents of the selected option’s () brackets, i.e. 123.45, 99.99 or 4.50?
So far I have:
$("select#target option:selected").contents();
That just gives Zero ($123.45) or equivalent though. How can I filter it further to get the number I’m looking for?
Thanks in advance.
I think that the most idiomatic way to do this with jQuery is
Regular expressions are usually the best way to match text in Javascript, and this style of code conforms to the usual jQuery method chaining style, doesn’t introduce any superfluous local variables, and doesn’t include an ugly index dereferencing operation. I’ve also written the selector in more compact form.