I have found various scripts and put together to try and achieve this.

The script below should register event listeners on load, but I am trying to do it for when I need multiple items with the same class name, e.g. a tooltip, or to ensure every button on the site had an Ajax loading image applied.

I believe I have everything sorted except 'the add_event_multiple_classes' function in my script.

Please help complete this and is this the best way to do it? It seem a lot easy to just use inline JS considering how much code this is taking.

if (typeof getElementsByClassName !== "function") {
  function getElementsByClassName(findClass, parent) {
    parent = parent || document;
    var elements = parent.getElementsByTagName('*');
    var matching = [];
    for(var i = 0, elementsLength = elements.length; i < elementsLength; i++){
      var regex = new RegExp('\b' + findClass + '\b')
      if (elements[i].className.match(regex)) {
    return matching;

if (window.addEventListener)
addEvent = function(ob, type, fn ) {
**ob.addEventListener(type, fn, false );
else if (document.attachEvent)
addEvent = function(ob, type, fn ) {
**var eProp = type + fn;
**ob['e'+eProp] = fn;
**ob[eProp] = function(){ob['e'+eProp]( window.event );};
**ob.attachEvent( 'on'+type, ob[eProp]);

function on_load_scripts() {
	//set some event listeners
	addEvent(document.getElementById('an-id'), 'click', some_javascript);
	add_event_multiple_classes ( 'submit-button', 'click', button_loading_image );

function add_event_multiple_classes(class, action, fn) {

	//somehow get hold of all the classes with the provided class name and convert so
	//they can all be referenced and have an event listener registered to them.
        // What about searching for all matching class names and adding a unique id next to it (how would I do this?)

	/// ??????????????????????????

	for/foreach() { // for every class with the 'class' name

		addEvent(xxxxxxxxxxx, action, fn);



function some_javascript() {

	//perform action


function button_loading_image() {

	//perform action


addEvent(window, 'load', on_load_scripts);