Hello, I have a select that is the following:
<form id="frmadd">
<label >Seleccionar producto</label>
<br>
<select type="number" name="buscadorvivoop" id="buscadorvivoop" class="form-control input-sm" style="width:260px; " onchange="data(this);">
<option value="0">ninguno</option>
<?php
while($ver=mysqli_fetch_row($result)):
?>
<option value="<?php echo $ver[0]; ?>" data-img="<?php echo $ver[2]; ?>">
<?php echo $ver[1] ?>
</option>
<?php
endwhile;
?>
</select>
<br>
<br>
<label>Cantidad a agregar</label>
<input type="number" name="cantidad" id="cantidad" class="form-control input-sm" style="width:260px;" value="0" size="15" onclick="if(this.value=='0') this.value=''" onblur="if(this.value=='') this.value='0'" onfocusout="addtabletemp('frmadd')"/>
<br>
</form>
To try to obtain the attribute and know which option is selected, I use the attribute onchange()
to send said element to the data() function:
<script>
function data(element){
var url = element.getAttribute('data-img');
if(url==null){
document.getElementById("imgpro").style.display = 'none';
}else{
document.getElementById("imgpro").src=url;
document.getElementById("imgpro").style.display = '';
}
}
</script>
but I don't get the attribute. I hope you can help me
You must use this to get to the selected option :
or this other within the javascript:
where what we do is use the selectedIndex property to delimit the option that is being selected.