1

I'm trying to move away from jQuery for my everyday site functionality, and I'm having a little bit of trouble with the onclick event. I'd like to put together a function like jQuery's .click(), but simply using document.getElementsByTagName and adding a func onclick won't work.

The question then is how would one add a single function to fire onclick to all elements in the list object returned by querying document.getElementsByTagName('h4')

EDIT: Just in case someone finds this and would like some code, here's what I did:

var headings = document.getElementsByTagName('h4')
    
for (var g in headings) {
    headings[g].onclick = function() {
        //code
    }
}

1 Answer 1

1

You need to loop through the list and pass the event to each item. I think there is no simpler way to do this, expect you need a library like jQuery or you write your own eventManager...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.