Reputation: 1323
I'm trying to standardize hacks in a library to use JavaScript to customize an Enterprise Platform that is very difficult to write client side code. Basically what I am trying to do, is allow a user to pass in the a reference to the data they want and regardless of how it's stored, I will try to get it using a couple of pre-defined methods. I know this process isn't pretty, but there aren't any alternatives for what we're trying to do.
I'm using jQuery 1.6.2 and bound to this due to dependencies of other libraries.
The following line:
if ($('input[id^=' + lineItemAttributeId + ']').length > 0) {
is giving the following error: Object doesn't support this property or method in IE8, but no other platform.
Services.prototype.getAttributeValue = function(docNum, variableName, defaultValue) {
if (docNum == "1") {
var quoteItemAttributeId = variableName;
if ($('input[id*=' + quoteItemAttributeId + ']').length > 0) {
return $('input[id*=' + quoteItemAttributeId + ']').val();
} else if ($('textarea[id*=' + quoteItemAttributeId + ']').length > 0) {
return $('textarea[id*=' + quoteItemAttributeId + ']').val();
} else if ($('select[name*=' + quoteItemAttributeId + ']').length > 0) {
return $('select[name*=' + quoteItemAttributeId + ']').find("option:selected").val();
} else {
return defaultValue;
}
} else {
var lineItemAttributeId = docNum + "_" + variableName;
if ($('input[id^=' + lineItemAttributeId + ']').length > 0) {
return $('input[id^=' + lineItemAttributeId + ']').val();
} else if ($('textarea[id*=' + lineItemAttributeId + ']').length > 0) {
return $('textarea[id*=' + lineItemAttributeId + ']').val();
} else if ($('select[name*=' + lineItemAttributeId + ']').length > 0) {
return $('select[name*=' + lineItemAttributeId + ']').find("option:selected").val();
} else {
return defaultValue;
}
}
};
I have tried:
if ($('input[id=' + lineItemAttributeId + ']').length > 0) {
if ($('input[id*=' + lineItemAttributeId + ']').length > 0) {
with similar results.
Any idea on what I am doing wrong?
Upvotes: 0
Views: 67
Reputation: 18233
In general the safest way to use the jQuery attribute selectors is to wrap the inner argument in quotation marks, especially when it can be dynamically valued. This accounts for issues arising from spaces, special characters, or other oddities which can cause your jQuery statement to be interpreted incorrectly. According to the documentation the unquoted format only works (reliably) for single word strings. So replace your attribute-equals selector with this: $('input[id="' + lineItemAttributeId + '"]').length
.
Upvotes: 1