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 sesction we will create the structure by using only HTML in the second section we will desing 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 desing 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= </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= <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:
Recommended Posts:
- How to change the background color after clicking the button in JavaScript ?
- Sound generation on clicking the button using JavaScript
- How to trigger a file download when clicking an HTML button or JavaScript?
- How to send row data when clicking button using javascript?
- Open a link without clicking on it using JavaScript
- How to display confirmation dialog when clicking an <a> link using JavaScript / jQuery ?
- How to set smooth scroll after clicking the link using JavaScript ?
- How to Change the Text of a Button using jQuery?
- How to change an input button image using CSS?
- CSS | Animation to Change the Hover State of a Button/Image
- How to avoid dropdown menu to close menu items on clicking inside ?
- How to make a Bootstrap 4 accordion collapse when clicking the whole header div ?
- How to trigger HTML button after hitting enter button in textbox using JavaScript ?
- How to change the href value of <a> tag after click on button using JavaScript ?
- Introduction to HTML CSS | Learn to Design your First Website in Just 1 Week
- JavaScript | Change the text of a span element
- How to change text color depending on background color using JavaScript?
- How to change the text of a label using JavaScript ?
- How to Change Image Dynamically when User Scrolls using JavaScript ?
- How to Create an Effect to Change Button Color using HTML and CSS ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


