Getting 'cannot read property' offsetwidth 'undefined bootstrap

I am getting offsetwidth

undefined despite adding an active class and also defining my jquery right above my js function. I have to use carousel for my job and I tried the layout in practice. My first active image is displayed and after I click next I get this error in the console and the images are not sliding.

  <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

  <title>mycooking</title>

</head>
<body>
  <!--indicatiors-->
  <div class="container">
    <div class="row-fluid">
      <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
          <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
          <li data-target="#myCarousel" data-slide-to="1"></li>
          <li data-target="#myCarousel" data-slide-to="2"></li>
        </ol>
        <!--wrapper for slides-->
        <div class="carousel-inner" role="listbox">
          <div class="item-active">
            <img class="first-slide" src="/img/dosa.jpg" alt="dosa" />
          </div>
          <div class="item">
            <img class="second-slide" src="/img/idli.jpg" alt="idli" />
          </div>
          <div class="item">
            <img class="third-slide" src="img/vada.jpg" alt="vada" />
          </div>
        </div>
        <!--Left and right controls-->
        <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
          <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
          <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
      </div>
    </div>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" type="text/javascript"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

  <script type="text/javascript">
    jQuery(document).ready(function () {
      $("#myCarousel").carousel({
          interval: 1200
      })
    });
  </script>
</body>
</html>

      

+3


source to share


1 answer


Change this line in your HTML:

<div class="item-active">

      

to



<div class="item active">

      

This hyphen should not be there. If you need a class item-active

, add it after these two for example <div class="item active item-active">

. item

and the active

bootstrap classes are used to run the carousel. Without them, on this first slide, you'll get this error.

NTN, -Ted

+5


source







All Articles