I would like to use the following function only on the “a” elements contained in the div “myDiv”.
This is the code I have been using so far:
$$('a[class="active"]').each(function(element) {
element.removeClassName("active");
});
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can do that two ways:
1) Look up
myDivand then useElement#select:or
2) Use an ID selector with a descendant selector with
$$:If you want only direct children (not descendants), you could use a child selector (note the
>):Off-topic #1: The usual way to write
a[class="active"]in a selector would bea.active, e.g., “$('myDiv').select('a.active').each(...);“.Off-topic #2: You might also look at the
invokefunction, which is for calling the same function on each item in an Enumerable object (so for instance, “$('myDiv').select('a[class="active"]').invoke('removeClassName', 'active');“).invokeis nice, short, and expressive, but it’s slower than doing it yourself as you did. (It only matters when you get into thousands of elements, though.)