Search code examples
javascriptjqueryfunctiononload-event

my function to change opacity not working


I have <img id="token" src="/files/images/token.png" /> that I'm attempting to change opacity with window.onload if $tokens value is 0. the $tokens value does read correctly and displays below the image with how many there are. this is my script:

<script>
window.onload=function(){
if($tokens == '0'){
var ntoken= document.getElementById("token");
   ntoken.style.opacity='0.4';
   ntoken.style.filter='alpha(opacity=40)';
 }
}
</script>

did I forget something?


Solution

  • Following @adeneo's remark, you might be mixing up PHP and javascript variables. It's a bit nasty but try this:

    <script>
    window.onload=function(){
    if(<?php echo $tokens; ?> == '0'){
    var ntoken= document.getElementById("token");
       ntoken.style.opacity='0.4';
       ntoken.style.filter='alpha(opacity=40)';
     }
    }
    </script>
    

    c.f. How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?>