-1

So I would like to change the class name of a jQuery object.

This is what I have:

var container = $('#container');

Then in the following function I would like to change the classname to newstate

function setstate(newstate) {
    state = newstate;
    //change container.classname to setstate
    currentsate = state;
}

I have looked up how to do this and tried the toggleClass and removeClass then addClass methods but neither seem to be working.

Also, in this function I need to get the position right, will these lines work?

function init() {
    var temp = container.offset();
    xoffset = temp.left;
    yoffset = temp.top;
    fontsize = container.offsetHeight / 1.5;
    linewidth = container.offsetHeight / 19;

    setstate('intro');
  }

Thank you.

VickiT05
  • 101
  • 2
  • 10
  • Have you checked [the jQuery](http://api.jquery.com/addClass/) [API documentation](http://api.jquery.com/removeClass/)? – Pointy Nov 16 '13 at 01:06
  • 1
    A duplicate [here](http://stackoverflow.com/questions/3452778/jquery-change-class-name) – Edper Nov 16 '13 at 01:08

2 Answers2

0

How about something like this?

var container = $('#container');

function changeClass(newClass){

    container.attr('class',newClass);
}


changeClass('myNewClass');

This example will erase the existing classes...if you want to add a class use container.addClass(newClass);

Villarrealized
  • 867
  • 1
  • 6
  • 13
0

you need to do this:

$("selector").toggleClass("a").toggleClass("b"); 

check this fiddle http://jsfiddle.net/victorrseloy/8cfu6/ to see an example