Ok, this code works fine
<html>
<body>
<p>This is question.</p>
<img src="http://cliparts.co/cliparts/qTB/5yd/qTB5ydpzc.gif" alt="show answer" height="42"
width="42" id="showHideImg" onclick="myFunction()">
<p id="showHide">This answer.</p>
<script type="text/javascript">
function myFunction() {
if (document.getElementById("showHide").style.display=="none")
{
document.getElementById("showHide").style.display="block";
}
else
{
document.getElementById("showHide").style.display="none";
}
}
</script>
</body>
</html>
However, in the real app, I got many images & <p> like the followings:
<p>This is question1.</p>
<img src="http://cliparts.co/cliparts/qTB/5yd/qTB5ydpzc.gif" alt="show answer" height="42"
width="42" id="showHideImg" onclick="myFunction()">
<p id="showHide1">This answer1.</p>
<p>This is question2.</p>
<img src="http://cliparts.co/cliparts/qTB/5yd/qTB5ydpzc.gif" alt="show answer" height="42"
width="42" id="showHideImg" onclick="myFunction()">
<p id="showHide2">This answer2.</p>
.......question 3... 4....5...
My question is that how to modify the myFunction() so that it can dynamically apply for many images
I tried like the following:
<p>This is question1.</p>
<img src="http://cliparts.co/cliparts/qTB/5yd/qTB5ydpzc.gif" alt="show answer" height="42"
width="42" id="showHideImg" onclick="myFunction(1)">
<p id="showHide1">This answer1.</p>
<script type="text/javascript">
function myFunction(var no) {
if (document.getElementById("showHide"+no).style.display=="none")
{
document.getElementById("showHide"+no).style.display="block";
}
else
{
document.getElementById("showHide"+no).style.display="none";
}
}
But it doesn't work
var. You should only haveno, notvar no.