By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Setting an element's class w/ js

P: n/a
Hi,

I'm trying to set the class of an element on my page w/ javascript like
this:

function ResetNavigation(element)
{

element.className = "active";
}

However, the effects of the active class are not showing up. When I add
this code to the above snippet, it is confirmed that the active is
being set as the element's class value.
alert(element.className);

Why won't my style render in the browser?

Sep 12 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
eg****@gmail.com wrote:
it is confirmed that the active is
being set as the element's class value.
Why won't my style render in the browser?
Probably something to do with specificity, and nothing to do with JS at all,
but lacking a URL its rather hard to say for sure.

--
David Dorward <http://blog.dorward.me.uk/ <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Sep 12 '06 #2

P: n/a
Here, take a look:

http://www.eggce.com

<a href="http://www.eggce.com">http://www.eggce.com</a>
David Dorward wrote:
eg****@gmail.com wrote:
it is confirmed that the active is
being set as the element's class value.
Why won't my style render in the browser?

Probably something to do with specificity, and nothing to do with JS at all,
but lacking a URL its rather hard to say for sure.

--
David Dorward <http://blog.dorward.me.uk/ <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Sep 12 '06 #3

P: n/a
eg****@gmail.com wrote:
I'm trying to set the class of an element on my page
w/ javascript like this:

function ResetNavigation(element)
{
element.className = "active";
}

However, the effects of the active class are not showing
up. When I add this code to the above snippet, it is
confirmed that the active is being set as the element's
class value.

alert(element.className);

Why won't my style render in the browser?
How should anyone know? If, for example, the value you pass to your
function as - element - is not actually an Element then setting its -
className - would not be expected to have any visible effect, but there
is nothing here to tell anyone what values may be being passed to the
function. Then the CSS you are using may have no viable manifestation
(either at all or in the context in which you use it). It may also be
the case that "active" is a problematic name to give a class in CSS, as
the active pseudo-class exists.

Without an demonstration of what you actually are doing any suggested
case can only be speculation.

Richard.
Sep 12 '06 #4

P: n/a
eg****@gmail.com wrote:
Here, take a look:

http://www.eggce.com

<a href="http://www.eggce.com">http://www.eggce.com</a>
Please don't top post.

As predicted, the style attribute is more specific then a class selector, so
the background colour from the style attribute wins.

--
David Dorward <http://blog.dorward.me.uk/ <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Sep 12 '06 #5

P: n/a
eg****@gmail.com wrote:
Here, take a look:

http://www.eggce.com
The - ResetNavigation - function on that page is:-

function ResetNavigation(element)
{
alert(element.className);
link.className = "active";
}

- and so is actually assigning to the className property of an
undeclared variable named - link -, and that is not going to have any
visible manifestation except the error in the javascript console.
David Dorward wrote:
eg****@gmail.com wrote:
<snip>

Please do not top-post to comp.lang.javascript.

Richard.

Sep 12 '06 #6

P: n/a
JRS: In article <ee*******************@news.demon.co.uk>, dated Tue, 12
Sep 2006 23:49:33 remote, seen in news:comp.lang.javascript, David
Dorward <do*****@yahoo.composted :
>eg****@gmail.com wrote:
>Here, take a look:

http://www.eggce.com

<a href="http://www.eggce.com">http://www.eggce.com</a>

Please don't top post.

Please do not use jargon ("top post") which the target is not likely to
understand - you are addressing a user of both gmail and googlegroups -
without providing an adequate reference to a reasonable explanation. It
is both unfriendly and generally futile.

You can cite (with URL) FAQ 2.3 paras 1 & 6, or the appropriate part of
some other reference such as in my sig below.

Be satisfied that the jargon term can be found in the reference, either
by indicating its position therein or at least by checking that a Ctrl-F
search for the exact jargon term will find it.

Richard writes "Please do not top-post...", bur Ctrl-F will not find
"top-post" in his FAQ, and the term is not in RFC 1855.

--
John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 4
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Sep 14 '06 #7

P: n/a
Dr John Stockton said the following on 9/14/2006 10:22 AM:

Richard writes "Please do not top-post...", bur Ctrl-F will not find
"top-post" in his FAQ, and the term is not in RFC 1855.
Richard has an FAQ? And all this time I thought it was the c.l.j FAQ and
he was only the Editor of that FAQ.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 14 '06 #8

P: n/a
Dr John Stockton wrote:
Please do not use jargon ("top post") which the target is not likely to
understand - you are addressing a user of both gmail and googlegroups -
So probably they can use one of Google's other services to find
http://www.google.com/search?q=top+post

--
David Dorward <http://blog.dorward.me.uk/ <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Sep 14 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.