How to Display Image Base on List in JavaScript

How to Display Image Base on List in JavaScript

Introduction

In this tutorial we will create a How to Display Image Base on List in JavaScript. This tutorial purpose is to teach you can display an image by clicking the list. This will tackle all the basic function that will display your background image. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that use images for your interface. I will give my best to provide you the easiest way of creating this program Display Background Image. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display images and list. To create this simply copy and write it into your text editor, then save it 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.                 <style>
  7.                         ul{
  8.                                 padding:0;
  9.                         }
  10.                         ul li{
  11.                                 display:inline;
  12.                         }
  13.                 </style>
  14.         </head>
  15.         <nav class="navbar navbar-default">
  16.                 <div class="container-fluid">
  17.                         <a class="navbar-brand" href="https://sourcecodeter.com">Sourcecodester</a>
  18.                 </div>
  19.         </nav>
  20.         <div class="col-md-3"></div>
  21.         <div class="col-md-6 well">
  22.                 <h3 class="text-primary">How to Display Image Base on List</h3>
  23.                 <hr style="border-top:1px dotted #ccc;">
  24.                 <h4>My List</h4>
  25.                         <ul>
  26.                                 <li style="cursor:pointer;" onclick="displayImage('naruto.png');">Naruto,</li>
  27.                                 <li style="cursor:pointer;" onclick="displayImage('bleach.jpeg');">Bleach,</li>
  28.                                 <li style="cursor:pointer;" onclick="displayImage('onepiece.jpg');">One Piece,</li>
  29.                                 <li style="cursor:pointer;" onclick="displayImage('dragonball.jpg');">Dragon Ball,</li>
  30.                                 <li style="cursor:pointer;" onclick="displayImage('hunterxhunter.jpg');">Hunter x Hunter</li>
  31.                 </ul>
  32.                 <div id="result">
  33.                         <center><h1>BLANK IMAGE</h1></center>
  34.                 </div>
  35.         </div>
  36. <script src="script.js"></script>
  37. </body>
  38. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will change your background image dynamically base on the chosen list. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function displayImage(img){
  2.         var result=document.getElementById('result');
  3.         result.innerHTML="<img src='images/"+img+"' width='100%' height='350px'/>";
  4. }

The code above is very simple and understandable, we will only create one method and we will name it displayImage(). This function will dynamically change your background image when an event is trigger. To change the image you just set the value for the image and make sure your image name in folder is the same as image value.

Output:

The How to Display Image Base on List in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Display Image Base on List in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment