Adding javascript to every occurrence of a certain HTML element?

What your looking for is most likely "event binding. " This can be done via your script rather than embedded in the HTML code. There are lots of different ways to accomplish such a task, here is one of them using "td" as in your example.

Up vote 1 down vote favorite share g+ share fb share tw.

I was wondering if there was a way of adding javascript in every occurrence of a certain HTML tag, eg. At the moment I just have it in every single one of my td tags in my table, but there must be a cleaner way. Something like adding javascript in the way CSS adds formatting. Javascript html javascript-events link|improve this question edited Apr 13 '10 at 13:34 asked Apr 7 '10 at 18:20Acorn7,68311032 92% accept rate.

– hunter Apr 7 '10 at 18:21 1 I mean adding things like events to elements, which I assume are "script tags". (sorry, I'm an absolute beginner so don't know the terminology) – Acorn Apr 7 '10 at 18:24.

What your looking for is most likely "event binding. " This can be done via your script rather than embedded in the HTML code. There are lots of different ways to accomplish such a task, here is one of them using "td" as in your example.

Var items = document. GetElementsByTagName("td"); for (var i=0; I Onmousedown = YourMouseDownFunction; }.

You want jQuery. See jQuery.org This can be accomplished using a "selector" (jquery term).

Add an event listener (See also: Quirks Mode on events and on event listeners) to your document looking for mousedown events and filter it on the basis of the originating element. There is a good answer here on Stackoverflow as well.

Those are really useful links! I get the impression that the filtering method isn't ideal though as if you have a mouseover event then whenever you mouseover any element in the page it will check to see if it is an element that should trigger a function. – Acorn Apr 7 '10 at 19:55 @Acorn - I'm glad the links helped!

And you are absolutely right about the filtering method, if you set the event listener on the body! I believe you could place the event listener on the table or tbody element, to reduce the amount of checking the browser would have to do. – Sean Vieira Apr 7 '10 at 21:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions