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.
For updated question:
I think this is what you're after, since the element since to be a previous sibling or contained in one:
You can test it out here.
For previous question: Use the
.class
selector, like this:You can also select elements that only have multiple classes by chaining them, for example:
Would only select elements that had all 3 classes.
jQuery selectors are nothing but strings, HTML attributes are nothing but strings and regular JavaScript has string operators and functions.
Here's a little (and meaningless) code snippet that puts these ideas together:
It's nice as a proof of concept, but you should double-check that your design is the simplest one.