working with html and javascript and changing the properties of html document but this is not working in my case?

Viewed 37

here I am trying to change the image source property with the help of javascript but it is not working my case how can I change this .some one suggest me change in code.so that is can work in my case.

this is the CSS file for formatting the HTML code. here I am using both logos from the internet. also changing the background and background is changed but imaged is not changed.

      function hello() {
        console.log(document.getElementsByTagName("img").src);
        document.getElementsByTagName("img").src =
          "https://www.facebook.com/images/fb_icon_325x325.png";
        document.body.style.background = "red";
      }
h2 {
  text-align: center;
}
body {
  margin: auto;
  padding: 5px;
}
#maincontent {
  text-align: center;
}
.btn {
  text-align: center;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="door.css" />
    <script src="main.js"></script>
    <title>Door</title>
  </head>
  <body>
    <h2 text-align="center">Open The Door</h2>
    <div id="maincontent">
      <img
        src="https://cfcdnpull-creativefreedoml.netdna-ssl.com/wp-content/uploads/2017/06/Twitter-logo-2012.png"
        id="hero"
        width="400px"
        height="600px"
      />
    </div>
    <div class="btn">
      <button id="workbtn" onclick="hello();">Open Door</button>
    </div>

  </body>
</html>

2 Answers

Get the elements id instead. document.getElementById("hero").src = "https://www.facebook.com/images/fb_icon_325x325.png"; This way you are specific in which element you are trying to get. Because you using a tag, you need to specify which child of the tag you are getting. could use document.getElementsByTagName("img").item(0); or document.getElementsByTagName("img")[0] or be more specific and get it by its id.

The [0] is the first occurrence of the tag on the page, numerical increments of items in code start with 0, if there were two, the key would be [1].

h2 {
  text-align: center;
}
body {
  margin: auto;
  padding: 5px;
}
#maincontent {
  text-align: center;
}
.btn {
  text-align: center;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="door.css" />
    <script src="main.js"></script>
    <title>Door</title>
  </head>
  <body>
    <h2 text-align="center">Open The Door</h2>
    <div id="maincontent">
      <img
        src="https://cfcdnpull-creativefreedoml.netdna-ssl.com/wp-content/uploads/2017/06/Twitter-logo-2012.png"
        id="hero"
        width="400px"
        height="600px"
      />
    </div>
    <div class="btn">
      <button id="workbtn" onclick="hello();">Open Door</button>
    </div>
    <script>
      function hello() {
        document.getElementById("hero").src =
          "https://www.facebook.com/images/fb_icon_325x325.png";
        document.body.style.background = "red";
      }
    </script>
  </body>
</html>

The error was that getelementsbyTagName returns an HTMLCollection list which is an array-like object.

Thus if you want to access the element , you have to use the arr[0] style.

function hello() {
        console.log(document.getElementsByTagName("img")[0].src);
        document.getElementsByTagName("img")[0].src =
          "https://www.facebook.com/images/fb_icon_325x325.png";
        document.body.style.background = "red";
      }

Code

      h2 {
        text-align: center;
      }
      body {
        margin: auto;
        padding: 5px;
      }
      #maincontent {
        text-align: center;
      }
      .btn {
        text-align: center;
      }
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="door.css" />
    <script src="main.js"></script>
    <title>Door</title>
  </head>

  <head>
   
  </head>

  <body>
    <h2 text-align="center">Open The Door</h2>
    <div id="maincontent">
      <img
        src="https://cfcdnpull-creativefreedoml.netdna-ssl.com/wp-content/uploads/2017/06/Twitter-logo-2012.png"
        id="hero"
        width="400px"
        height="600px"
      />
    </div>
    <div class="btn">
      <button id="workbtn" onclick="hello();">Open Door</button>
    </div>
    <script>
      function hello() {
        console.log(document.getElementsByTagName("img")[0].src);
        document.getElementsByTagName("img")[0].src =
          "https://www.facebook.com/images/fb_icon_325x325.png";
        document.body.style.background = "red";
      }
    </script>
  </body>
</html>

Related