Reputation: 769
I need to use a jquery function to add a css class I have created to textboxes on a form. This class will change the border of the textbox to red, to indicate that there is an error in the textbox and that the user cannot submit until the error is fixed. Here is my current code:
function validateRequired() {
var obj = $('.validate');
var message = "";
if (obj == null) {
return message;
}
obj.each(function (index, elem) {
if (elem.value == "") {
message += splitCamelCase(elem.id);
elem.attr("class", "error");
}
});
return message;
}
For whatever reason, though I've used this in other points in the code to check and set attributes, I keep getting an error saying:
The property or method (elem.attr("class", "error");) is unsupported.
How can I set the border of the textbox to red using jquery?
Upvotes: 1
Views: 2546
Reputation: 18462
elem
is the raw HTMLElement. You need to wrap it in $(elem)
, or do elem.className = 'error'
Upvotes: 3
Reputation: 161
You could try to do instead of attribute:
//...
message += splitCamelCase(elem.id);
$('this').addClass("error");
//...
Upvotes: 0