1

I have the div which is generated when page loads as below.

<div class="redactor_ redactor_editor"></div>

How can I add a class .example onto it via jquery like below?

<div class="redactor_ redactor_editor example"></div>

Thank you very much!

4
  • 3
    Sorry but I think you need to take a basic tutorial about jQuery. Commented Dec 19, 2012 at 6:10
  • You haven't accepted any answer yet. Are you looking for replacing the previous class with a new class? Commented Dec 19, 2012 at 6:53
  • I'll try to create a working copy of my code on jsfiddle.net for everyone to see what is going on in my code... Commented Dec 19, 2012 at 16:40
  • Here is a copy of my working code. jsfiddle.net/bYSs3/4 The purpose of my question is to change the vertical scrollbar by adding a class .example that I believe to be added on the div with class .redactor_ redactor_editor Commented Dec 19, 2012 at 17:16

5 Answers 5

4

Try addClass this,

$('.redactor_').addClass('example');

just select element using any selector

$(selector).addClass('example');

demo

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

Comments

2

you can add class to element by using the .addClass()

syntex

.addClass( className )

className One or more class names to be added to the class attribute of each matched element.

so now this is you want

$('.redactor_ redactor_editor').addClass('example');

1 Comment

does it work with the space in the class name after the first underscore sign? (redactor_ redactor_editor)
0

try this :

<pre>
 $(window).load(function () { 
   $('.redactor_ redactor_editor').addClass('example');
 });
</pre>

reference : addClass

Comments

0

You can set the class regardless of what it was by using .attr() like this,

If the html is like this,

<div class="redactor_ redactor_editor"></div>

then the selector will be like the following,

$(document).ready(function() {
 $('.redactor_redactor_editor').attr('class', 'redactor_redactor_editor example');
});

Since your div tag's class name has a space after the underscore sign(i.e. redactor_ ) the following way must be applied to select the element.(<div class="redactor_ redactor_editor"></div>)

$(document).ready(function() {
   $("div[class='redactor_ redactor_editor']").attr('class', 'redactor_ redactor_editor example');
});

Comments

0

If you are looking for adding a new css class to an element that already has a class then-

DEMO

HTML-

<div class="one">example</div>​

CSS-

.one{color:red;}
.two{color:green}​

jQuery-

$(function(){
 $('.one').addClass('two');
});​

If you are looking for removing the old css class and add a new class-

DEMO

jQuery-

$(function() {
    $('.one').removeClass('one').addClass('two');
});​

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.