OnClick without jQuery

JavascriptOnclick

Javascript Problem Overview


How to make onclick without jQuery, with no extra code in HTML, such as:

<a href="#" onclick="tramtramtram">

Just using an external js file?

<script type="text/javascript" src="functions.js"></script>

I need to replace this code:

$("a.scroll-up, a.scroll-down").click(function(){
    SNavigate($(this).attr("href").substr(7));return false;
});

Javascript Solutions


Solution 1 - Javascript

When this anchor will contain only one function to handle on click, than you can just write

document.getElementById('anchorID').onclick=function(){/* some code */}

otherwise, you have to use DOM method addEventListener

function clickHandler(){ /* some code */ }
var anchor = document.getElementById('anchorID');
if(anchor.addEventListener) // DOM method
  anchor.addEventListener('click', clickHandler, false);
else if(anchor.attachEvent) // this is for IE, because it doesn't support addEventListener
   anchor.attachEvent('onclick', function(){ return clickHandler.apply(anchor, [window.event]}); // this strange part for making the keyword 'this' indicate the clicked anchor

also remember to call the above code when all elements are loaded (eg. on window.onload)

-- edit

I see you added some details. If you want to replace the code below

$("a.scroll-up, a.scroll-down").click(function(){SNavigate($(this).attr("href").substr(7));return false;});

with sth that doesn't use jQuery, this should do the job

function addEvent(obj, type, fn) {
        if (obj.addEventListener)
                obj.addEventListener(type, fn, false);
        else if (obj.attachEvent)
                obj.attachEvent('on' + type, function() { return fn.apply(obj, [window.event]);});
}
addEvent(window, 'load', function(){
   for(var i=0, a=document.anchors, l=a.length; i<l;++i){
      if(a[i].className == 'scroll-up' || a[i].className == 'scroll-down'){
         addEvent(a[i], 'click', function(e){ SNavigate(this.href.substr(7)); e.returnValue=false; if(e.preventDefault)e.preventDefault();return false});
      }
   }
});

Solution 2 - Javascript

how about this :

document.getElementById("lady").onclick = function () {alert('onclick');};

Solution 3 - Javascript

W3C suggests:

element.addEventListener('click',doSomething,false)

Microsoft uses:

element.attachEvent('onclick',doSomething)

You can do some feature detection and determine which call to use.

From http://www.quirksmode.org/js/events_advanced.html.

Solution 4 - Javascript

With no extra code in the html, here's a way to do it:

<html><body><head><script>  // This script could be in an external JS file
function my_handler(elt) {
    alert("Yay!");
    return false;
}

function setup() {
    document.getElementById('lady').onclick = my_handler;
}

window.onload = setup;
</script></head>
<body><a href='#' id='lady'>Test</a></body>
</html>

Solution 5 - Javascript

<script>
 function tramtramtram(){
     alert("tram it!");
 }
</script>


<a href="#" onclick="tramtramtram()">tramtram</a>

Solution 6 - Javascript

var el =document.getElementById('lady'); el.addEventListener( "click", tramtramtram, false );

Solution 7 - Javascript

Execute this script when document loads (replacing alert with your stuff):

var rgLinks = document.getElementsByTagName("A");

for (var x=0; x<rgLinks.length; x++) {
	el = rgLinks[x];

	if (el.className == 'scroll-down' || el.className == 'scroll-down')
		{
		el.onclick = function () {alert('onclick');};
		}
	}

Maybe that is what you are asking about...

Solution 8 - Javascript

Just use the onclick attribute.

<a href="#" onclick="functionName(someParam)">

Or to do it from javascript,

document.getElementById("anchorID").onclick = function(){
   some code
};

Solution 9 - Javascript

You need to use getElementById to add the onclick handler. However you need to initiate your code somewhere. Without jQuery you can only use the body onload handler:

<body onload="myinitfunction()">

myinitfunction can be in an external js file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeView Question on Stackoverflow
Solution 1 - JavascriptRafaelView Answer on Stackoverflow
Solution 2 - JavascriptCanavarView Answer on Stackoverflow
Solution 3 - JavascriptCorbin MarchView Answer on Stackoverflow
Solution 4 - JavascriptRichieHindleView Answer on Stackoverflow
Solution 5 - JavascriptbchhunView Answer on Stackoverflow
Solution 6 - Javascriptka3751View Answer on Stackoverflow
Solution 7 - Javascriptbuti-oxaView Answer on Stackoverflow
Solution 8 - JavascriptnullView Answer on Stackoverflow
Solution 9 - JavascriptkgiannakakisView Answer on Stackoverflow