Reputation: 2269
I'm getting this error in IE8 and IE7 for some reason. I'm looping through all keys within my object and it keeps telling me Object doesn't support this property or method
on this on:
var inVal = $(inType + "#" + inName).val().trim();
The entire block is below:
for (var key in inputs) { // find all keys "brand", "name", etc. in our inputs object
var errors = false;
if (inputs.hasOwnProperty(key)) { // focus on our obj. not all others on page
var inType = inputs[key].inputType;
var inName = inputs[key].inputName;
var inVal = $(inType + "#" + inName).val().trim(); // construct input field
if (inVal == '' || inVal == null) {
errors = true;
$('#' + inName + '-error').html('');
$('#' + inName + '-error').hide().fadeIn().html(inputs[key].errorMsg);
$(inType + '#' + inName).focus();
$('#modal-loadable-inner').scrollTop(inputs[key].scrollTop);
return;
} else { // user corrected error
errors = false;
$('#' + inName + '-error').html(''); // remove error txt
}
}
}
Someone posted this on Doug Crockford's jslint boards and he responded with:
for (key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { ... } }
hasOwnProperty should have been an operator, not a method, because being a method, it is prone to these sorts of problems. But it is what it is, so you have to work around that.
Works fine in Chrome, FF, Safari, Opera, etc.. as usual. Any idea on a workaround?
Upvotes: 1
Views: 4316
Reputation: 38131
.val()
may not be returning a string, so calling .trim()
on it is probably causing the problem. Try using an intermediate variable and checking it is not undefined before calling .toString().trim()
on it (assuming .trim()
is a method you added to strings in Javascript - the jQuery method is $.trim()
).
Upvotes: 1
Reputation: 4433
I think it's caused by your trim()
as string do not have this method.
Try this instead
var inVal = $.trim($(inType + "#" + inName).val());
Upvotes: 4