I am trying to select a background image according to the selected option in a onChange
function, but it happens that it (the image) is only applied on the first page if I NÃO
use the function onChange
to the address of the image shows on all pages normally, the problem occurs when implementing the select option and onChange function, it follows code and images with and without the function that I am using:
SEM function:
COMfunction:
Inspectingtheelements,Icheckedthefollowing:
Mycode:
functionscript
<script>functionmyFunction(){varx=document.getElementById("mySelect").value;
document.getElementById("imagem").src = "img/gab" + x + ".png"
}
</script>
CSS
<style>
html { font-size: 12pt;
}
.folha { align:center;background-color: #ccc; padding: 0.5em;
}
.a4_vertical { width: 793px; height: 1122px;
margin-left: auto;
margin-right: auto;
}
.a4_horizontal { width: 1122px; height: 793px;
margin-left: auto;
margin-right: auto;
}
#container {
display: inline-block;
position: relative;
}
#container figcaption {
position: absolute;
top: 94px;
left: 87px;
color: black;
}
<title>FOLHA DE RESPOSTA</title>
</head>
<body>
Select with onChange
<div align="center">
<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>
This is where I call the background image, if in place of id="image" I use
src=...
works but I need to use aselect
withonChange
How it works:
<div><img align="center" src="img/gab1.png" width="600px" height="auto" /></div>
So não
Works:
<div><img align="center" id="imagem" width="600px" height="auto" /></div>
Edit
Just added this to Fernando's answer and it worked for me.
window.onload=function(){
//código aqui
}