LukasA
LukasA

Reputation: 109

jquery background-image change

I want to load images into a div on my html but it doesn't work, if i change the var image = $("#slider") to $("body") it works, but then the whole body fades in and out and thats not what i wanted.

i guess there is something wrong with the identifier?

    $(document).ready(function(){
    var count = 0;
    var images = ["/mvc_tut/views/index/images/bg1.jpg","/mvc_tut/views/index/images/bg2.jpg","/mvc_tut/views/index/images/bg3.jpg","/mvc_tut/views/index/images/bg4.jpg"];
    var image = $("body");

    image.css("background-image","url("+images[0]+")");

    setInterval(function(){
      image.fadeOut(1000, function(){
        image.css("background-image","url("+images[count++]+")");
        image.fadeIn(1000);
      });
      if(count == images.length)
      {
        count=0;
      }
    },3000);
});

thats the html

<div class="container">
  <div id="#slider">
  <div class="row">
    <div class="col-md-6 homeleft">
    </div>
    <div class="col-md-3 col-md-offset-2">
      <div class="login">
        <form action="index/login" method="POST">
          <input type="text" class="form-control" name="userLogin" placeholder="Nutzername">
          <input type="password" class="form-control" name="passwortLogin" placeholder="Passwort">
          <button type="submit" class="btn btn-default">Anmelden</button>
        </form>
      </div>
      <div class="register">
        <form action="index/register" method="POST">
          <input type="text" class="form-control" name="userReg" placeholder="Nutzername">
          <input type="text" class="form-control" name="emailReg" placeholder="Email">
          <input type="password" class="form-control" name="passwortReg" placeholder="Passwort">
          <button type="sumbit" class="btn btn-default">Registrieren</button>
        </form>
      </div>
    </div>
  </div>
</div>
</div>

Upvotes: 0

Views: 56

Answers (2)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167240

The problem is here:

<div id="#slider">
<!-------^ // Please remove the #

The HTML elements should not have an id starting with #. Moreover, from the documentation:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Upvotes: 1

S&#248;ren Pedersen
S&#248;ren Pedersen

Reputation: 11

I see you have "#slider" in the id value. You should not have a '#' in there. '#' is only used by jQuery to identify a select on id.

Upvotes: 1

Related Questions