1

i have a function like this in js

function rmStyle(parent){
    var rmStyle=document.getElementById(parent).getElementsByTagName("a");
    for (i=0;i<rmStyle.length;i++){rmStyle[i].className="";}}

i'd like to shorten the function, since i'm using jQuery... so, what should i write in jQuery?

3
  • 2
    jQuery is a JS framework - you can just use that function. Oh, and for future reference...indent! Readable code is happy code. Commented Jan 27, 2011 at 4:20
  • @Andrew: I think the point was code reduction. Commented Jan 27, 2011 at 4:22
  • @patrick dw - Yeah, I misinterpreted his question. I was thinking of it as "How do I do this in jQuery?", instead of "How can I make this shorter with jQuery?". Commented Jan 27, 2011 at 4:26

2 Answers 2

5
$('#' + parent).find('a').removeClass();

The .removeClass() method will remove all classes on a selected item if no class name is passed.

Sign up to request clarification or add additional context in comments.

1 Comment

I did not know removeClass could be called with no args. Very nice. +1
5
function rmStyle(parent) {
    $('#' + parent).find('a').attr('class','');
}

This will get the element with the parent id, find all a descendants, and set their class attribute to ''.

2 Comments

Or $('#'+parent+' a').attr(...);
@Phrogz: Yeah, I debated over that, but decided against it for clarity. Glad you mentioned it though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.