JavaScript - Change Background Template Based On Days
Submitted by razormist on Friday, March 8, 2019 - 21:35.
In this tutorial we will create a Change Background Template Based On Days using JavaScript. This code can dynamically change the template of the page based on the given days. It will display the images that are store in the images directory when the user submitted the choice. A user-friendly program so that you can modify and understand it without an ease.
We will be using JavaScript as a server-side scripting language that interpret a program to extend a whole new level of HTML experience. A programming language that is characterized as a dynamic modern approach for a new programming technique.
There you have it we successfully created a Change Background Template Based On Days using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding
Getting started:
First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="form-inline">
- <select id="days" class="form-control">
- </select>
- </div>
- <br />
- <div id="display">
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will change the template of the page when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.- function changeTemplate(){
- var days = document.getElementById("days");
- if(days.value === ""){
- alert("Please select an item first");
- }else{
- switch(days.value){
- case "Monday":
- document.getElementById("display").innerHTML = '<center><img src="images/monday.jpg" width="600"/></center>';
- break;
- case "Tuesday":
- document.getElementById("display").innerHTML = '<center><img src="images/tuesday.jpg" width="600"/></center>';
- break;
- case "Wednesday":
- document.getElementById("display").innerHTML = '<center><img src="images/wednesday.jpg" width="600"/></center>';
- break;
- case "Thursday":
- document.getElementById("display").innerHTML = '<center><img src="images/thursday.jpg" width="600"/></center>';
- break;
- case "Friday":
- document.getElementById("display").innerHTML = '<center><img src="images/friday.jpg" width="600"/></center>';
- break;
- case "Saturday":
- document.getElementById("display").innerHTML = '<center><img src="images/saturday.jpeg" width="600"/></center>';
- break;
- case "Sunday":
- document.getElementById("display").innerHTML = '<center><img src="images/sunday.jpg" width="600" height="380"/></center>';
- break;
- }
- }
- }
Add new comment
- 39 views