-1

I am trying to make a slideshow-like thing with JavaScript for a website and it isn't going well. What I want it to do is after one click change it so that it returns correctly.

Here is the code:

function previous_slide()
{
    document.getElementById('banner').className = 'banner_2';
}

I'd like to make that single function switch between its current action and this one:

document.getElementById('banner').className = 'banner_1';

so that when you press the previous button once it changes the style to banner_2 and then when you press it again it returns to banner_1.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Search for "toggle" and you will find a bunch of solutions. – Bergi Jun 07 '12 at 19:15
  • Note you can [format lines as code](http://meta.stackexchange.com/questions/22186/) by indenting them four spaces; backticks are for inline code. The "{}" button in the editor toolbar does this for you. Veger did it for you this time, but next time try it out. Click the orange question mark in the editor toolbar for more information and tips on formatting. `
    ` are generally non-semantic and should be used in rare instances. They're also unnecessary on SO.
    – outis Jun 07 '12 at 19:16
  • Consider picking a [meaningful username](http://tinyurl.com/so-hints). One advantage to this is others can use [at-replies](http://meta.stackexchange.com/questions/43019/how-do-comment-replies-work) and you'll get a notification that someone has addressed you in a comment. Make sure you read the FAQ. It's likely that SO is structured differently than you expect (for one thing, it's a Q&A site, not a forum). – outis Jun 07 '12 at 19:17
  • possible duplicate of [JavaScript (jQuery?): Toggle multiple classes with one click](http://stackoverflow.com/q/6886091/), [click .toggle add/remove class](http://stackoverflow.com/q/2720665/), [Javascript toggle state function](http://stackoverflow.com/q/8723152/), [Toggle SPAN Class along with this div toggle](http://stackoverflow.com/q/9233374/) – outis Jun 07 '12 at 19:28
  • Sorry about the duplicates and the not noticing i got a reply but thank you for helping me everyone. :) Also i did have a go at making it like the proper code and all that but i just want feeling great looked at the instructions a blanked :P but thank you :) – user1443016 Jun 08 '12 at 20:52

2 Answers2

1
previous_slide = toggle( [function(){
    document.getElementById('banner').className = 'banner_2'
}, function() {
    document.getElementById('banner').className = 'banner_1' 
}]);

Code for toggle

function toggle(functions) {
    var i = 0;
    return function() {
        return functions[i++ % functions.length].apply( this, arguments );
    }
}
Esailija
  • 138,174
  • 23
  • 272
  • 326
0
function previous_slide()
{
  if (document.getElementById('banner').className == 'banner_2') {
      document.getElementById('banner').className = 'banner_1' 
  }
  else {
      document.getElementById('banner').className = 'banner_2'
  } 
}
Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50