The problem is due to HTML entities; the "<
" is seen by the browser as "<
".
The same could be said for the example provided by bobince; please note that the following does not work with jQuery 1.32 on Win + FF3:
var select= document.getElementById('SomeDropdown');
for (var i= select.options.length; i-->0;) {
if (select.options[i].value=="a'b]<p>") {
alert('found it');
}
}
However, changing the entity to a literal will indeed find the desired value:
var select= document.getElementById('SomeDropdown');
for (var i= select.options.length; i-->0;) {
if (select.options[i].value=="a'b]<p>") {
alert('found it');
}
}
Of course, there is a problem here, as the value that you're specifying is not the exact value that you're looking for. This can also be corrected with the addition of a helper function:
function html_entity_decode(str) {
var decoder = document.createElement('textarea');
decoder.innerHTML = str;
return decoder.value;
}
All together now:
var srcValue = html_entity_decode("a'b]<p>");
var select= document.getElementById('SomeDropdown');
for (var i= select.options.length; i-->0;) {
if (select.options[i].value == srcValue) {
alert('found it');
}
}
Any now, the input value that you're searching for exactly matches the value of the select element.
This can also be written using jQuery methods:
var srcValue = html_entity_decode("a'b]<p>");
$($('#SomeDropdown').attr('options')).each(function(){
if (this.value == srcValue)
{
$(this).remove();
}
});
And then finally, as a plugin since they are so easy to make:
jQuery.fn.removeByValue = function( val )
{
var decoder = document.createElement('textarea');
decoder.innerHTML = val;
var srcValue = decoder.value;
$( $(this)[0].options ).each(function(){
if (this.value == srcValue){
$(this).remove();
}
});
return this;
};
$('#SomeDropdown').removeByValue("a'b]<p>");