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

How to set the class attribute?

P: n/a
Hello,

On this way I set the target attribute for a hyperlink.

var the_link = sel.parentElement();
the_link.target = '_blank';

Now I also want to set the class attribute.
When I use the word class I get errors.

Can somebody tell me how to set the class attribute?

Thanks!
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Arjen wrote:
Hello,

On this way I set the target attribute for a hyperlink.

var the_link = sel.parentElement();
the_link.target = '_blank';

Now I also want to set the class attribute.
When I use the word class I get errors.

Can somebody tell me how to set the class attribute?

Thanks!


className

Mick
Jul 23 '05 #2

P: n/a
"Mick White" <mw******@BOGUSrochester.rr.com> schreef in bericht
news:wI*********************@twister.nyroc.rr.com. ..
Arjen wrote:
Hello,

On this way I set the target attribute for a hyperlink.

var the_link = sel.parentElement();
the_link.target = '_blank';

Now I also want to set the class attribute.
When I use the word class I get errors.

Can somebody tell me how to set the class attribute?

Thanks!


className

Mick


Mick,

Then it adds classname as attributename... but it must be class.
If tried to run your solution and it doesn't work... other options?

Thanks.

Jul 23 '05 #3

P: n/a
Arjen wrote:
> Can somebody tell me how to set the class attribute?
className
Then it adds classname as attributename...
Not if you do it right.
but it must be class.
class is a reserved keyword in ECMAScript, so className is used instead.
If tried to run your solution and it doesn't work... other options?


Present us with a URL so we can see the code you tried to use. Then we might
be able to tell why it isn't working.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Jul 23 '05 #4

P: n/a
"Arjen" <bo*****@hotmail.com> writes:
className
Then it adds classname as attributename...
No. The HTML attribute is called "class". The corresponding DOM
property is called "className".
but it must be class. If tried to run your solution and it doesn't
work... other options?


Try it again, it is correct:
---
<style type="text/css">
.foo {color:red;background:yellow;}
.bar {color:yellow;background:red;}
</style>
<div class="foo" onclick="this.className='bar'">CLICK</div>
---

//L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #5

P: n/a

"David Dorward" <do*****@yahoo.com> schreef in bericht
news:c8*******************@news.demon.co.uk...
Arjen wrote:
> Can somebody tell me how to set the class attribute? className
Then it adds classname as attributename...


Not if you do it right.
but it must be class.


class is a reserved keyword in ECMAScript, so className is used instead.
If tried to run your solution and it doesn't work... other options?


Present us with a URL so we can see the code you tried to use. Then we

might be able to tell why it isn't working.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>


Here it is:
var the_link = sel.parentElement();
the_link.target = '_blank';
the_link.classname = 'DCELink';

Thanks!
Jul 23 '05 #6

P: n/a
"Arjen" <bo*****@hotmail.com> writes:
the_link.classname = 'DCELink';


Javascript is case sensitive. It should be
the_link.className = 'DCELink';

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #7

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:br**********@hotpop.com...
"Arjen" <bo*****@hotmail.com> writes:
the_link.classname = 'DCELink';
Javascript is case sensitive. It should be
the_link.className = 'DCELink';

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'


Okay, thanks! ;-)
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.