JavaScript - Change Background Template Based On Days

Submitted by: 
Language: 
Visitors have accessed this post 113 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Change Background Template Based On Days</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="form-inline">
  18. <label>Days</label>
  19. <select id="days" class="form-control">
  20. <option value="">Select an option</option>
  21. <option value="Monday">Monday</option>
  22. <option value="Tuesday">Tuesday</option>
  23. <option value="Wednesday">Wednesday</option>
  24. <option value="Thursday">Thursday</option>
  25. <option value="Friday">Friday</option>
  26. <option value="Saturday">Saturday</option>
  27. <option value="Sunday">Sunday</option>
  28. </select>
  29. <button type="button" class="btn btn-primary" onclick="changeTemplate();"><span class="glyphicon glyphicon-random"></span> Change Template</button>
  30. </div>
  31. <br />
  32. <div id="display">
  33. <center><h3>Template here...</h3></center>
  34. </div>
  35. </div>
  36. <script src="js/script.js"></script>
  37. </body>
  38. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.