Help with onchange function

Asked

Viewed 88 times

0

I’m trying to use the function onchange, I can even pick up the value, but I can’t make use of the value obtained, Follow the code:

function myFunction() {
  var x = document.getElementById("mySelect").value;
  document.getElementById("demo").innerHTML = x;
}
<div>
  <select id="mySelect" onchange="myFunction()">
    <option value="1">1 Questão
    <option value="2">2 Questões
    <option value="3">3 Questões
    <option value="4">4 Questões
  </select>
</div>
<b id="demo"></b> <!--aqui eu imprimo o valor de x corretamente-->

I would like to use the value of X as a complement to the image address

tried so:

  <img src="img/gab<?echo '<b id="demo"></b>'?>.png" />

Thus

  <img src="img/gab<script>x</script>.png" />

and

  <img src="img/gab<script>$x</script>.png" />

I don’t know how to do

1 answer

3


Try to do it that way:

function myFunction() {
    var x = document.getElementById("mySelect").value;
    document.getElementById("demo").innerHTML = x;
    document.getElementById("imagem").src = "img/gab" + x + ".png"
}
<select id="mySelect" onchange="myFunction()">
  <option value="1">1 Questão
  <option value="2">2 Questões
  <option value="3">3 Questões
  <option value="4">4 Questões
</select>

<b id="demo"></b>

<img id="imagem">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.