0

I am trying to switch my div class using a timer, and I'm always switching between 2 classes.

Here is the beginning of the div code

<div id="this" class="photobanner">

You can check the JQuery function and timer I made below

function autoAddClass(){
    if( $( '#this' ).hasClass( 'first' ))
        $('.first').addClass('last');
    if{$('#this').hasClass('last'))
        $('.last').addClass('first');
    }
	setTimeout(autoAddClass, 1000);

This is not working

3
  • 2
    If your code is called first, you have only class photobanner not class first and not class last. So both if statements return false.
    – Jens
    Commented Jul 31, 2015 at 11:51
  • jsfiddle.net/arunpjohny/szgzgfse/1 Commented Jul 31, 2015 at 11:57
  • 1
    Thanks, I have used photobanner instead of first in the div, now it works thanks to you and @soosmca
    – Seifounage
    Commented Jul 31, 2015 at 12:16

3 Answers 3

2

Actually second if statement you wrongly put '{' instead of '('. For verey second you have to change the class means Use setInterval

Your function be using toggleClass in jquery

function autoAddClass(){
    $('#this').toggleClass("first last");
}
$('#this').addClass('first');
setInterval(autoAddClass, 1000);

Fiddle

4
  • That almost works, but I still have one small problem: I'm using two animations, last & first, first makes the pictures in the gallery scroll to the right, and first makes them scroll to the left, the problem is that when the class is switched to last, the pictures are not moving, they're just frozen (I have tested both classes before using timers and they both work fine)
    – Seifounage
    Commented Jul 31, 2015 at 12:00
  • I would guess you're animation takes longer than 1000 milli seconds.
    – Rokie
    Commented Jul 31, 2015 at 12:06
  • I apologize, it's working now, i had a missing bracket somewhere, thanks!
    – Seifounage
    Commented Jul 31, 2015 at 12:13
  • @SeifHassine - Glad it helps Commented Jul 31, 2015 at 12:34
1

function autoAddClass(){
    if( $( '#this' ).hasClass( 'first' ))
        $('.first').addClass('last');
    if{$('#this').hasClass('last'))
        $('.last').addClass('first');
    }
	setTimeout(autoAddClass, 1000);

Your second if-clause is invalid. Try: if ($('#this').hasClass('last')) {...}

Also, I think you should be using toggleClass, because after the first autoAddClass, your element will have both classes.

3
  • The last bracket } refers to the autoAddclass function, not the if statement -> EDIT: You are right, oups
    – Seifounage
    Commented Jul 31, 2015 at 11:52
  • @SeifHassine your if-clause start like if{..., that doesn't work
    – G_hi3
    Commented Jul 31, 2015 at 11:54
  • Yes you are right, but I don't know why the code is still not working
    – Seifounage
    Commented Jul 31, 2015 at 12:02
1

Adding new classes to the elements is fine, but shouldn't you remove the existing class before adding a new one.

JS CODE:

setInterval(function(){
autoAddClass();}
, 1000);
     function autoAddClass() {
    console.log('timer elapsed');
    if ($('#container').hasClass('green')) {
        $('.green').removeClass('green').addClass('red');
    console.log('green');
    } else {
        $('.red').removeClass('red').addClass('green');
     console.log('red');
    }
}

Live demo @ JSFiddle:http://jsfiddle.net/dreamweiver/mpLq6f7b/2/

Note:Never use this keyword for variables/id/class selectors, its a bad practice also this is reserved keyword in javascript

1
  • 1
    I edited (pending review) your code to fix a { that was in a wrong position from OP code (on the second if). Now your code should work has OP requested Commented Jul 31, 2015 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.