How to change the text and image by just clicking a button in JavaScript ?

18 May, 2022
The image and text can be changed by using javascript functions and then calling the functions by clicking a button. We will done that into 3 sections., in the first section we will create the structure by using only HTML in the second section we will design minimally to make it attractive by using simple CSS and in the third section we will add the JavaScript code to perform the task
HTML Code: In this section we create the structure plus we will add Bootstrap cdn link for the button design and the fonts.
HTML
<!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="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">   </head>   <body>      <div class="container">         <div class="row">            <div class="col-md-3"></div>            <div class="col-md-6">               <h1 id="message">                   Hii! GeeksforGeeks people               </h1>               <img  id="myImage"                     src="img/photo1.jpg"  >            </div>            <div class="col-md-3"></div>         </div>      </div>      <div class="button">         <button class="btn btn-warning"                 onclick=before();>             Before         </button>         <button  class="btn btn-success"                  onclick=afterr();>             Afterr         </button>      </div>   </body></html>
CSS Code: In this section we will use some basic CSS styling to make it look as normal as possible.
CSS
<style>    img{    height: 500px;    width: 450px;    }    h1{    color: darkgreen;    margin-top: 20px;    font-family: Comic Sans MS, cursive, sans-serif;    }    .button{    margin-left: 45%;    }</style>
  
Javascript Code: In this section, we will define JavaScript functions and we have taken two images, which are present in a separate “img” folder.
Javascript
<script>    function before(){        document.getElementById('myImage')        .src="img/photo1.jpg";        document.getElementById('message')        .innerHTML="Hii! GeeksforGeeks people";    }         function afterr(){        document.getElementById('myImage')        .src="img/photo2.jpg";        document.getElementById('message')        .innerHTML="Bye! GeeksforGeeks people";    }</script>
  
Complete Code: It is the combination of above codes of HTMl, CSS and JavaScript. 
html
<!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="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">      <style>         img{         height: 500px;         width: 450px;         }         h1{         color: darkgreen;         margin-top: 20px;         font-family: Comic Sans MS, cursive, sans-serif;         }         .button{         margin-left: 45%;         }      </style>      <script>         function before(){             document.getElementById('myImage')             .src="img/photo1.jpg";             document.getElementById('message')             .innerHTML="Hii! GeeksforGeeks people";         }                   function afterr(){             document.getElementById('myImage')             .src="img/photo2.jpg";             document.getElementById('message')             .innerHTML="Bye! GeeksforGeeks people";         }      </script>   </head>   <body>      <div class="container">         <div class="row">            <div class="col-md-3"></div>            <div class="col-md-6">               <h1 id="message">                   Hii! GeeksforGeeks people               </h1>               <img  id="myImage"                     src="img/photo1.jpg"  >            </div>            <div class="col-md-3"></div>         </div>      </div>      <div class="button">         <button class="btn btn-warning"                 onclick=before();>             Before         </button>         <button  class="btn btn-success"                  onclick=afterr();>             Afterr         </button>      </div>   </body></html>
 Output:  
HTML and CSS both are foundation of webpages. HTML is used for webpage development by structuring websites, web apps and CSS used for styling websites and webapps. JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn more about HTML and CSS from the links given below:
 HTML Tutorial and HTML Examples.
 CSS Tutorial and CSS Examples.
 JavaScript Tutorial and JavaScript Examples.
sumitgumber28
adnanirshad158
hardikkoriintern
CSS-Misc
HTML-Misc
JavaScript-Misc
Bootstrap
CSS
HTML
JavaScript
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to set Bootstrap Timepicker using datetimepicker library ?
How to Show Images on Click using HTML ?
How to Use Bootstrap with React?
How to Add Image into Dropdown List for each items ?
How to change the background color of the active nav-item?
How to update Node.js and NPM to next version ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
How to create footer to stay at the bottom of a Web page?
CSS to put icon inside an input element in a form