Search code examples
javascripthtmlimagesrc

Javascript img.src from html select


I'm trying to get an image source from an option tag, and use it as a .src in javascript. The problem I face is that I already have a value on my select tag.

<form action="" method="post" name="properties">
    <input type="number" name="speed" value="" id="spd"><br>
    <input type="number" name="angle" value="" id="ang"><br>

    <select name="location">
        <option name="earth" value="9.810">Jorda</option>
        <option name="moon"  value="1.622">Månen</option>
        <option name="mars"  value="3.771" >Mars</option>
        <option name="mercury" value="3.701">Merkur</option>
    </select>

</form>

I've gotten the values to JS using.

document.properties.location.value

But I also need to get an image path to JS through the same option. How should I do this?


Solution

  • you should put that in your JS code , passing only the value by param you should make a case statement

    var ImagePath ="";
    switch (document.properties.location.value)
    {
        case  "9.810":
            ImagePath = "http:\\.....\image1.jpg";
        break;
        case  '1.622':
            ImagePath = "http:\\.....\image2.jpg";
        break;
        case  '3.771':
            ImagePath = "http:\\.....\image3.jpg";
        break;
        case  '3.701':
            ImagePath = "http:\\.....\image4.jpg";
        break;
    

    }

    after this, imagepath should hold the path to the desired image.