Forum Moderators: open

Message Too Old, No Replies

How can you attach an event to all links in a table?

         

vaxop

9:19 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



<table id='mTable'>
Content
</table>
<script language = 'Javascript'>
var cont = document.getElementById("mTable");
var links = cont.getElementsByTagName("a");
for (var i=0,len=links.length; i < len; i++){
How can i attach an event to all these links?
}
</script>

Anyone know?
the function i want to attach is onmouseover:
function ss(theHREF){var url = /url=(.+)/.exec(theHREF)[1];alert(url);}

Purple Martin

4:34 am on Jun 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick guess:

links[i].onMouseOver = "ss(" + links[i].href + ");";

Notice that onMouseOver is case-sensitive - everything in JavaScript is.

p.s. We have a dedicated JavaScript forum here ;)
[webmasterworld.com...]

-----------
Thanks for mentioning the Javascript Forum.
I decided to move the thread to a new home.

[edited by: tedster at 5:33 am (utc) on June 15, 2004]

Bernard Marx

7:59 am on Jun 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it is case-sensitive ... so we need
[blue]onmouseover[/blue]
.

The best thing to do is direct it to a link method:

for (var i=0,len=links.length; i < len; i++){ 
links[i].onmouseover = ss
}

// function looks like this...

function ss()
{
var url = /url=(.+)/.exec([blue]this[/blue].href)[1];
alert(url);
}

..although I don't understand the regular expression part myself.

Rambo Tribble

12:56 pm on Jun 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to throw in a little clarification, onMouseOver is the correct use of case for a reference to the method that is internal to JavaScript, since the language is case sensitive and uses that form of mixed case in identifiers. HTML usually doesn't care about case, but XHTML demands all elements and attributes be specified in lower case. In the instance cited, onmouseover is being used as an (X)HTML attribute, so lower case is preferred, though not necessarily required, depending on the
!DOCTYPE declaration of the page.

Bernard Marx

1:15 pm on Jun 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



onMouseOver is the correct use of case for a reference to the method that is internal to JavaScript

Surely you mean "isn't"

Purple Martin

12:31 am on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, he means "is".

When it's part of some JavaScript code, use onMouseOver.
When it's an XHTML attribute, use onmouseover.

In the case cited, and in the example code I gave, we're using JavaScript code (between <script> tags), and therefore we need onMouseOver.

It's confusing, isn't it! :)

Rambo Tribble

2:20 am on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My bad, PM. You're right, in the instance cited it is described as within a script block.

Bernard Marx

10:40 am on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure who Rambo's agreeing with now!

Martin, I strongly disagree (is the best way of putting it). It seems you are adding the event handler as an attribute, and concatenating an appropriate value to put in it. This isn't really worthwhile. More importantly, it doesn't work. Have you tried this?

When it's part of some JavaScript code, use onMouseOver.
When it's an XHTML attribute, use onmouseover.

- Use onmouseover all the time. It's easier.

Rambo Tribble

3:41 pm on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to throw in a clarification of the clarification, even within a JavaScript block it is going to depend on what kind of object you are addressing which syntax is correct. Just like addressing the size of a font will sometimes be "font-size" in the CSS convention; it will be "fontSize" when addressing it as a JavaScript object attribute. The same should apply to onmouseover/onMouseOver, except of course when coding handlers for events, where in some cases it will be just "mouseover". Had enough?

Best bet? "When in doubt, try it out."