I would like to dynamically display an image after the user has selected it. The different options are generated from a SQL query.
It works when I specify the image as a value:
<select id="SelectLogo" onchange="document.getElementById('logo').src='/images/'+this.value" >
<option value="img1.png">Image 1</option>
<option value="img2.png">Image 2</option>
<option value="img3.png">Image 3</option>
</select>
<img id="logo" src="">
But now I need to post an Id but not the image name. I have tried that:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="SelectLogo" onchange="document.getElementById('logo').src='/images/'+$(this).attr('data-img')" >
<option value="1" data-img="img1.png">Image 1</option>
<option value="2" data-img="img2.png">Image 2</option>
<option value="3" data-img="img3.png">Image 3</option>
</select>
<img id="logo" src="">
I got an error : undefined