471,073 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

How to set the class attribute?

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
7 6552
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
"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
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
"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

"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
"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

"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.

Similar topics

2 posts views Thread by Fernando Rodriguez | last post: by
3 posts views Thread by George Sakkis | last post: by
166 posts views Thread by Graham | last post: by
2 posts views Thread by Ewald R. de Wit | last post: by
reply views Thread by keith bannister via .NET 247 | last post: by
3 posts views Thread by antred | last post: by
1 post views Thread by chris.bahns | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.