How to set the class attribute?

A

Arjen

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!
 
M

Mick White

Arjen said:
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
 
A

Arjen

Mick White said:
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.
 
L

Lasse Reichstein Nielsen

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

Arjen

David Dorward said:
Not if you do it right.


class is a reserved keyword in ECMAScript, so className is used instead.


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.



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

Thanks!
 
D

David Dorward

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top