html - jQuery 1.8.2 - how do I grab an option from a select with only the text? -
simply put, using this:
<select id="test"> <option value="1">abc</option> <option value="2">def</option> </select> how can use jquery option[value="2"] using "def" - keep in mind using 1.8.2
thank you!
try :contains() selector:
$('#test option:contains("def")') note :contains() wildcard search. if need exact match, think you'll need iterate or filter options , check text(). here's 1 possibility:
console.log(getoptionbytext('test', 'def')); function getoptionbytext(id, value) { return $('#' + id).find('option').filter(function() { return $(this).text() == value; }); }
Comments
Post a Comment