Skip to content Skip to sidebar Skip to footer

Javascript Not Working With Html External Link

I'm using Notepad++, and I know for sure that it works when linking different files to the main HTML file.Here is the HTML code I'm using:

and you have to also use

return document.getElementById(id);

in Gid function

that's it.

Solution 2:

The problems have been covered in comment and other answers.

As you tagged this question with jQuery here is the simpler jQuery equivalent:

HTML:

<body><buttonid="testme">Click Me</button><divid="box"><divid="out"></div></div></body>

Code:

// Listen for click on the id="testme" button
$('#testme').click(function(){
     // $('#out') is a jquery wrapped version of the id="out" divvar$out = $('#out');
     // jQuery html reads/or writes innerHTML depending on parameters$out.html("testing" + "<br/>" + $out.html()); 
});

If the script precedes the elements it accesses in the page, you will need to wrap it in a DOM ready handler:

$(function(){
    // Listen for click on the id="testme" button
    $('#testme').click(function(){
         // $('#out') is a jquery wrapped version of the id="out" divvar$out = $('#out');
         // jQuery html reads/or writes innerHTML depending on parameters$out.html("testing" + "<br/>" + $out.html()); 
    });
});

Note: $(function(){}); is just a handy shortcut for $(document).ready(function(){});

Solution 3:

There are two updates that you need to do. First, replace this line:

<button onclick=log("testing");>Click Me</button>

with this line:

<buttononclick="log('testing'); return false;">Click Me</button>

This should prevent your page from posting back when you click the button. Then replace this line:

return getElementById(id);

with this line:

return document.getElementById(id);

because you don't have any valid context there.

Solution 4:

try to call the function onclick="log('testing')" like this it might work

Post a Comment for "Javascript Not Working With Html External Link"