Martynas JanuĊĦauskas Martynas JanuĊĦauskas - 7 months ago 9
Javascript Question

return not returning variable when Function call from img onclick

why does not return a variable? I need to work with the variable when it is call out during img oncklick function;
How can I change the code?

<p id="demo"></p>

<a href="a.php?action=sand"> <img onClick="TargaliuSand()" src="sandelis.png" style="width:120px;height:120px;">
</a>

<script>
var myVar2 = setInterval(myTimer2, 1000);
function argaliuSand() {
document.getElementById("demo").innerHTML = a;
}
function TargaliuSan(){
a = 1;
return a;
function myTimer2() {
if (a==1){argaliuSan()}
}

</script>

Answer

Your code changing the p element isn't in the function. It's immediately called. A "solution" would be:

<p id="demo"></p>

<img onclick='myFunction();' src="aple.jpg">

<script>
function myFunction() {
    var a=1;
    document.getElementById("demo").innerHTML = a;
}

</script>

But I strongly urge you to go to a correct tutorial site, like the MDN in order to learn the basics.