Reputation: 218
I'm currently using jQuery to match options in a drop-down to a string. That's working, no issues. User puts in a string, I strip out unwanted characters and/or abbreviate certain words, then find a matching option in the drop-down. However, I also need to check for the original string.
For example, say a user enters the string "Madison County Hospital". We'll call this strNameRaw. After I run it through the process, it becomes "Madison CNTY HSPTL", which we'll call strNameFixed. I then use the code below to find a match for the strNameFixed value:
$("select[title='Project Name'] option").filter(function() { return $(this).text() === strNameFixed }).prop('selected', true);
I've done a few google searches, but so far I haven't found a way to search for either string. I want a result of TRUE or "1" if either one is matched, not just the one I've processed.
How can I re-write this line so that it searches for EITHER string instead of just one?
Upvotes: 0
Views: 115
Reputation: 54741
If I understand correctly. You just need to match either and select first.
$("select[title='Project Name'] option")
.filter(function() {
var txt = $(this).text();
return txt === strNameFixed || txt === strNameRaw
})
.first()
.prop('selected', true);
Upvotes: 1