0
Staff I am trying to select a background image according to selected option in a function onChange
, but it turns out that it (the image) is only applied on the first page if I NÃO
use the function onChange
for the image address shows on all pages normally, the problem is when implementing the select option and onChange function, follows code and images with and without the function I am using:
WITHOUT the function:
WITH the function:
Inspecting the elements I checked the following:
My code:
function script
<script>
function myFunction() {
var x = 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>
Here is where I call the background imag, if in place of id="image" I use
src=...
works but I need to use aselect
withonChange
That’s how it works:
<div><img align="center" src="img/gab1.png" width="600px" height="auto" /></div>
Thus não
Works:
<div><img align="center" id="imagem" width="600px" height="auto" /></div>
Edit
Just add that to Fernando’s answer and it worked for me
window.onload=function(){
//código aqui
}
rsrs Fixed kkk
– Miguel Silva