Connecting Tech Pros Worldwide Forums | Help | Site Map

Overriding a href using onClick

VA
Guest
 
Posts: n/a
#1: Oct 31 '05
Firefox 1.0.7

If I launch Jesse Ruderman's Javascript shell

http://www.squarefree.com/2005/10/30...ript-shell-14/

on any web page and type in the shell

var mylink
for (j=0;j<document.links.length;j++) {
mylink=document.links[j];
if (mylink.href.match(/something/)) {
mylink.href="#";
mylink.onClick="alert('clicked');return false;";
}
}

I expected all my links having "something" in them to now popup my
alert box.

But nothing happened, no errors in the Javascript console, nothing
happens when I click on those links.

What gives?

Thanks


VA
Guest
 
Posts: n/a
#2: Oct 31 '05

re: Overriding a href using onClick


Lee wrote:[color=blue]
> Links don't have an onClick attribute.
> They do have an onclick attribute, but it should be assigned a
> reference to a function, not a string value.[/color]

So how would I modify my snippet above to do what I want it to do?

Thanks

Lee
Guest
 
Posts: n/a
#3: Oct 31 '05

re: Overriding a href using onClick


VA said:[color=blue]
>
>Firefox 1.0.7
>
>If I launch Jesse Ruderman's Javascript shell
>
>http://www.squarefree.com/2005/10/30...ript-shell-14/
>
>on any web page and type in the shell
>
>var mylink
>for (j=0;j<document.links.length;j++) {
> mylink=document.links[j];
> if (mylink.href.match(/something/)) {
> mylink.href="#";
> mylink.onClick="alert('clicked');return false;";
> }
>}
>
>I expected all my links having "something" in them to now popup my
>alert box.
>
>But nothing happened, no errors in the Javascript console, nothing
>happens when I click on those links.[/color]

Links don't have an onClick attribute.
They do have an onclick attribute, but it should be assigned a
reference to a function, not a string value.

Randy Webb
Guest
 
Posts: n/a
#4: Oct 31 '05

re: Overriding a href using onClick


VA said the following on 10/30/2005 11:39 PM:[color=blue]
> Lee wrote:
>[color=green]
>>Links don't have an onClick attribute.
>>They do have an onclick attribute, but it should be assigned a
>>reference to a function, not a string value.[/color]
>
>
> So how would I modify my snippet above to do what I want it to do?[/color]

You change onClick in your code to onclick

CaSe MaTtErS

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Andrew Poulos
Guest
 
Posts: n/a
#5: Oct 31 '05

re: Overriding a href using onClick


> var mylink[color=blue]
> for (j=0;j<document.links.length;j++) {
> mylink=document.links[j];
> if (mylink.href.match(/something/)) {
> mylink.href="#";
> mylink.onClick="alert('clicked');return false;";
> }
> }[/color]

You could try:

var mylink
for (var j = 0; j<document.links.length; j++) {
mylink = document.links[j];
if (mylink.href.match(/something/)) {
mylink.href = "#";
mylink.onclick = function() {
alert("clicked");
return false;
}
}
}


Andrew Poulos
Mick White
Guest
 
Posts: n/a
#6: Oct 31 '05

re: Overriding a href using onClick


VA wrote:
[color=blue]
> Lee wrote:
>[color=green]
>>Links don't have an onClick attribute.
>>They do have an onclick attribute, but it should be assigned a
>>reference to a function, not a string value.[/color]
>
>
> So how would I modify my snippet above to do what I want it to do?
>[/color]

mylink.onclick=function(){alert('clicked');return false;}

Mick

Closed Thread