0

I've the following HTML:

<li class="treeview" id="account_management">

So I want to target the element with id "account_management" and change it's class from "treeview" to "treeview active", so that I can then style my tree menu accordingly.

7
  • 2
    It's well worth your time to spend an hour or two reading through the jQuery API documentation. It really only takes that long, and it answers this question and hundreds more. Commented Oct 2, 2014 at 8:35
  • thanks @T.J.Crowder surely you can answer every single question on this site by saying read the docs? Not really helpful. I don't have any intention of learning jQuery at this moment in time as I don't really have any need for it other than this right now. There's no point in my writing poor code if someone who knows the mechanics of the language that is will to spend a minute to help me Commented Oct 2, 2014 at 8:39
  • 1
    The point is that if you will learn the very basic jQuery things, it will save your and other people time. Noone said that you should learn it completely. Commented Oct 2, 2014 at 8:45
  • 1
    @twigg: It wasn't an answer. It was a suggestion to help save your time and others'. And no, reading the API docs doesn't answer every question. Some questions are bigger than API calls. Some are actually about hard things. This is trivial, and a trivial glance at the API docs would show you this tempting addClass function. Commented Oct 2, 2014 at 8:46
  • 1
    @T.J.Crowder If the OP isn't interested in learning, next time suggest Google ;) Commented Oct 2, 2014 at 8:59

2 Answers 2

5

You can use jQuery addClass() to add the class active:

$("#account_management").addClass("active");

Or you can use Element.classList DOM API's add() method like:

document.getElementById("account_management").classList.add("active");

classList browser support @caniuse

for older versions, see this answer.

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

1 Comment

Yes, it is always sad when big work stays even without "accepted". By the way, mentioned answer was posted after 2.5 months - it was probably too late, even though it works correctly :)
1

In pure JavaScript you could do:

var d = document.getElementById("account_management");
d.className = d.className + " active";

If you already use jQuery in your project, I would recommend to make use of it like in T J's answer.

6 Comments

Using jQuery or not using jQuery has no relation to the language you're using (JavaScript). You mean "Using the DOM you could do:"
I know that jQuery is also JavaScript. By pure JavaScript I meant, without a framework.
@Regent: It's just this horrible, and anti-constructive, meme that somehow jQuery is something other than JavaScript. Again, $("#account_management").addClass("active") is "pure JavaScript" (using jQuery). The alternative above is pure JavaScript (using the DOM). Nothing whatsoever to do with the language.
@T.J.Crowder well, the idea is that "pure JS" is "using JS without additional libraries/plugins". It's just the way how you interpret word "pure".
@Regent: No, it's the way you interpret the word JavaScript, which has no place in that term. Pure DOM is what people actually mean. I'll stop banging on about it. :-)
|

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.