Possible Duplicate:
In jquery: Use attr(“class”) value as a selector?
I have fields with multiple classes. I want to use one of those classes as a selector, how do I do this?
generic example:
<input id="from"/><div class="error from">errormessage</div>
<input id="to"/>
<div><input id="when" /></div><div class="error when">errormessage</div>
Because ther is a div.error with the class from, I want to add a certain class to input#from and the same goes for the input called when.
Note that “when” is wrapped in a div. Several variations like this are necessary for the page. Therefore, tree-traversal is not a suitable solution.
or
Note the lack of white-space between the two class-names
.errorand.from, which selects only those elements with both of the given class-names.If there had been a space (
$('.error .from')) then this would select an element of classfromthat is a descendant of an element of class.error.You could, if you wanted to (though I can’t think why you would), use
hasClass():Though I think this is rather less efficient.