Skip to content Skip to sidebar Skip to footer

Javascript - Document.getelementbyid With Onclick

I'm trying to code a simple game in an HTML document just for fun. In it, there is an image which adds to your score when clicked. I created a simple if statement within the score-

Solution 1:

The onclick property is all lower-case, and accepts a function, not a string.

document.getElementById("test").onclick = foo2;

See also addEventListener.

Solution 2:

In JavaScript functions are objects.

document.getElementById('foo').onclick = function(){
    prompt('Hello world');
}

Solution 3:

window.onload = prepareButton;

functionprepareButton()
{ 
   document.getElementById('foo').onclick = function()
   {
       alert('you clicked me');
   }
}

<input id="foo" value="Click Me!" type="button" />

Solution 4:

Perhaps you might want to use "addEventListener"

document.getElementById("test").addEventListener('click',function ()
{
    foo2();
   }  ); 

Hope it's still useful for you

Solution 5:

Sometimes JavaScript is not activated. Try something like:

<!DOCTYPE html><html><head><scripttype="text/javascript"> <!--
      functionjActivator() {
        document.getElementById("demo").onclick = function() {myFunction()};
        document.getElementById("demo1").addEventListener("click", myFunction);
        }
      functionmyFunction( s ) {
        document.getElementById("myresult").innerHTML = s;
        }
    // --> </script><noscript>JavaScript deactivated.</noscript><styletype="text/css"></style></head><bodyonload="jActivator()"><ul><liid="demo">Click me -&gt; onclick.</li><liid="demo1">Click me -&gt; click event.</li><lionclick="myFunction('YOU CLICKED ME!')">Click me calling function.</li></ul><divid="myresult">&nbsp;</div></body></html>

If you use the code inside a page, where no access to is possible, remove and tags and try to use 'onload=()' in a picture inside the image tag '

Post a Comment for "Javascript - Document.getelementbyid With Onclick"