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>
    <button id="testme">Click Me</button>
    <div id="box">
        <div id="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" div
     var $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" div
         var $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:

<button onclick="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"