JavaScript - Change Background Template Based On Days

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.

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.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Change Background Template Based On Days</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="form-inline">
  17. <label>Days</label>
  18. <select id="days" class="form-control">
  19. <option value="">Select an option</option>
  20. <option value="Monday">Monday</option>
  21. <option value="Tuesday">Tuesday</option>
  22. <option value="Wednesday">Wednesday</option>
  23. <option value="Thursday">Thursday</option>
  24. <option value="Friday">Friday</option>
  25. <option value="Saturday">Saturday</option>
  26. <option value="Sunday">Sunday</option>
  27. </select>
  28. <button type="button" class="btn btn-primary" onclick="changeTemplate();"><span class="glyphicon glyphicon-random"></span> Change Template</button>
  29. </div>
  30. <br />
  31. <div id="display">
  32. <center><h3>Template here...</h3></center>
  33. </div>
  34. </div>
  35. <script src="js/script.js"></script>
  36. </body>
  37. </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.
  1. function changeTemplate(){
  2. var days = document.getElementById("days");
  3.  
  4. if(days.value === ""){
  5. alert("Please select an item first");
  6. }else{
  7. switch(days.value){
  8. case "Monday":
  9. document.getElementById("display").innerHTML = '<center><img src="images/monday.jpg" width="600"/></center>';
  10. break;
  11. case "Tuesday":
  12. document.getElementById("display").innerHTML = '<center><img src="images/tuesday.jpg" width="600"/></center>';
  13. break;
  14. case "Wednesday":
  15. document.getElementById("display").innerHTML = '<center><img src="images/wednesday.jpg" width="600"/></center>';
  16. break;
  17. case "Thursday":
  18. document.getElementById("display").innerHTML = '<center><img src="images/thursday.jpg" width="600"/></center>';
  19. break;
  20. case "Friday":
  21. document.getElementById("display").innerHTML = '<center><img src="images/friday.jpg" width="600"/></center>';
  22. break;
  23. case "Saturday":
  24. document.getElementById("display").innerHTML = '<center><img src="images/saturday.jpeg" width="600"/></center>';
  25. break;
  26. case "Sunday":
  27. document.getElementById("display").innerHTML = '<center><img src="images/sunday.jpg" width="600" height="380"/></center>';
  28. break;
  29. }
  30. }
  31. }
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

Add new comment