• We just launched and are currently in beta. Join us as we build and grow the community.

How to Display Image Base on List in JavaScript

Moonperson111

Code Fusion Specialist
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
147
Likes
133
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. <body

    >
  16. <nav

    class

    =

    "navbar navbar-default"

    >
  17. <div

    class

    =

    "container-fluid"

    >
  18. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodeter.com"

    >

    Sourcecodester</

    a

    >
  19. </

    div

    >
  20. </

    nav

    >
  21. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  22. <div

    class

    =

    "col-md-6 well"

    >
  23. <h3

    class

    =

    "text-primary"

    >

    How to Display Image Base on List</

    h3

    >
  24. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    >
  25. <h4

    >

    My List</

    h4

    >
  26. <ul

    >
  27. <li

    style

    =

    "cursor:pointer;"

    onclick

    =

    "displayImage('naruto.png');"

    >

    Naruto,</

    li

    >
  28. <li

    style

    =

    "cursor:pointer;"

    onclick

    =

    "displayImage('bleach.jpeg');"

    >

    Bleach,</

    li

    >
  29. <li

    style

    =

    "cursor:pointer;"

    onclick

    =

    "displayImage('onepiece.jpg');"

    >

    One Piece,</

    li

    >
  30. <li

    style

    =

    "cursor:pointer;"

    onclick

    =

    "displayImage('dragonball.jpg');"

    >

    Dragon Ball,</

    li

    >
  31. <li

    style

    =

    "cursor:pointer;"

    onclick

    =

    "displayImage('hunterxhunter.jpg');"

    >

    Hunter x Hunter</

    li

    >
  32. </

    ul

    >
  33. <div

    id

    =

    "result"

    >
  34. <center><h1

    >

    BLANK IMAGE</

    h1

    ></

    center>
  35. </

    div

    >
  36. </

    div

    >
  37. <script

    src

    =

    "script.js"

    ></

    script

    >
  38. </

    body

    >
  39. </

    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:

How%20to%20Display%20Image%20Base%20on%20List%20in%20JavaScript%201.png


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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top