0

HTML: It's really simple, just the slide with the buttons to change the image.

<img src="https://i.ytimg.com/vi/QJg2z3rRgSE/maxresdefault.jpg" id="myslides">
<ul id="mylist">
  <li><button class="btns" id="btn1">Previous</button></li>
  <li><button class="btns" id="btn2">Next</button></li>
</ul>

CSS: I don't need help here, I'm not much into design.

#myslides {
  height: 100%;
  width: 100%;
  margin-left: auto;
  margin-right: auto;
}

#mylist,
li {
  display: inline-block;
  position: relative;
  top: -5px;
  left: -20px
}

.btns {
  height: 50px;
  width: 200px;
  background: #666666;
  color: white;
  font-size: 20px;
}

JQuery: I have no clue what I'm doing wrong here. "image" can't be undefined because it's not "img" so why isn't the src change working?

$(docuent).ready(function() {
  var slide = $("#myslides");
  $("#btn2").click(function() {
    if (slide.attr("src") == "https://i.ytimg.com/vi/QJg2z3rRgSE/maxresdefault.jpg") {
      slide.attr("src", "https://techgage.com/wp-content/uploads/2015/11/The-Witcher-3-Wild-Hunt-at-3440x1440-Resolution.jpg");
    }
  });
});

$(docuent).ready(function() {
  var slide = $("#myslides");
  $("#btn2").click(function() {
    if (slide.attr("src") == "https://i.ytimg.com/vi/QJg2z3rRgSE/maxresdefault.jpg") {
      slide.attr("src", "https://techgage.com/wp-content/uploads/2015/11/The-Witcher-3-Wild-Hunt-at-3440x1440-Resolution.jpg");
    }
  });
});
#myslides {
  height: 100%;
  width: 100%;
  margin-left: auto;
  margin-right: auto;
}

#mylist,
li {
  display: inline-block;
  position: relative;
  top: -5px;
  left: -20px
}

.btns {
  height: 50px;
  width: 200px;
  background: #666666;
  color: white;
  font-size: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="https://i.ytimg.com/vi/QJg2z3rRgSE/maxresdefault.jpg" id="myslides">
<ul id="mylist">
  <li><button class="btns" id="btn1">Previous</button></li>
  <li><button class="btns" id="btn2">Next</button></li>
</ul>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • 6
    `$(docuent).ready(function() {` Spelling matters in programming. – CertainPerformance Aug 31 '18 at 04:40
  • 3
    `docuent` is not a valid object in JavaScript. Change it to `document`. – 31piy Aug 31 '18 at 04:41
  • 1
    typo-**docuent**.ready – NullPointer Aug 31 '18 at 04:41
  • 1
    @Filip Its happening because of typeO. `$(document).ready(function() { var slide = $("#myslides"); $("#btn2").click(function() { //console.log(slide.attr('src')); if (slide.attr("src") == "https://i.ytimg.com/vi/QJg2z3rRgSE/maxresdefault.jpg") { slide.attr("src", "https://techgage.com/wp-content/uploads/2015/11/The-Witcher-3-Wild-Hunt-at-3440x1440-Resolution.jpg"); } }); });` https://codepen.io/moshiuramit/pen/mGRrbx?editors=1111 – moshiuramit Aug 31 '18 at 04:58
  • 1
    only you need to $(docuent) to $(document) that sit. – dev_ramiz_1707 Aug 31 '18 at 05:31

2 Answers2

-1

your code is properly working but you need to change only $(docuent) to $(document)

hear you can see Click to see

$(document).ready(function() {  

you write here like below

$(docuent).ready(function() {  
dev_ramiz_1707
  • 671
  • 4
  • 20
-1

You misspelled document. If you're looking for a more scalable way to make a slider, you could try something like this:

$(document).ready(function () {
  var visibleSlide = $("#myslides");
  var slideList = [
    "https://i.ytimg.com/vi/QJg2z3rRgSE/maxresdefault.jpg",
    "https://techgage.com/wp-content/uploads/2015/11/The-Witcher-3-Wild-Hunt-at-3440x1440-Resolution.jpg"
  ];
  var currentSlideIndex = 0;

  $("#btn1").click(function () {
    changeSlide('previous');
  });

  $("#btn2").click(function () {
    changeSlide('next');
  });

  function changeSlide(direction) {
    switch(direction) {
      case 'previous':
        if (currentSlideIndex > 0) {
          currentSlideIndex--;          
        }

        break;

      case 'next':
        if (currentSlideIndex < slideList.length - 1) {
          currentSlideIndex++;
        }

        break;

      default:
        // no-op
    }

    visibleSlide.attr('src', slideList[currentSlideIndex]);
  }

});

See CodePen.

In the event that you don't have jQuery loaded onto the page, I've added it to the bottom of your HTML:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Tina
  • 1,186
  • 10
  • 11