0

I am trying to have an image swap with another when a user clicks a button on a webpage. Looking less for an answer and more for why the logic I am trying isn't working or what I am missing. Trying to solve with plain JS to get to grips with the language. Here is what I am trying:

<!DOCTYPE html>

<html lang="en">
    <head>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous">
             document.addEventListener('DOMContentLoaded', function(){
                let eng = document.querySelector("#eimg");
                let btn1 = document.querySelector("#eswitch");
                btn1.addEventListener('click', function(){
                    eng.src = 'England2.jpeg';
                })
             })
        </script>
        <link href="styles.css" rel="stylesheet">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>My Webpage</title>
    </head>
    <body>
        <div class="header">
            <h1 class="title">Travel</h1>
        </div>
        <div>
          <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="./index.html">Home</a></li>
              <li class="breadcrumb-item active" aria-current="page">Travel</li>
            </ol>
          </nav>
                <div class="card" style="width: 18rem;">
                    <img id="eimg" src="./England.jpeg" class="card-img-top" alt="Silhouette of England">
                    <div class="card-body">
                      <h5 class="card-title">England</h5>
                      <button id="eswitch" class="btn btn-primary">England</button>
                      </div>
                    </div>

    </body>
</html>
Shanahando
  • 61
  • 5
  • 1
    `eng.src = 'England2.jpeg'` Did you try `./Endgland2.jpeg` instead of `England2.jpeg`? – kanvil Oct 24 '22 at 00:52
  • 2
    Your code needs `` without `src` .. you can take a look at [Html script tag - can script be added when src is used](https://stackoverflow.com/questions/21825131/html-script-tag-can-script-be-added-when-src-is-used) – Mohamed-Yousef Oct 24 '22 at 01:01
  • @kanvil I tried that but no luck. Thanks for the suggestion though! – Shanahando Oct 24 '22 at 01:18
  • @Mohamed-Yousef Thank you! that worked. Thanks for linking the other answer too, that was going to be my next question. :) – Shanahando Oct 24 '22 at 01:20

1 Answers1

2

You will need to break out your JS into its own script block.

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous">
</script>
<script>
    document.addEventListener('DOMContentLoaded', function(){
        let eng = document.querySelector("#eimg");
        let btn1 = document.querySelector("#eswitch");
        btn1.addEventListener('click', function(){
            eng.src = 'England2.jpeg';
                console.log(eng)
            })
        })
</script>
Cyberdemon
  • 36
  • 2