Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ignore additional events

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
Hi again,

Have an unexpected complication.

<span onclick=&quot;myFunction()&quot;>
<img onclick=&quot;this.parentElement.click();return;&quot; class=&quot;myImage&quot;>
</span>

function myFunction(){

var tC;

if (event.srcElement.innerHTML){
tC = event.srcElement.children[0].className;
}

alert(tC);

}

---

This results in 2x alerts! Why? The first outputs myImage, the second is blank.

How best to filter out the junk? :p

----------
I'm willing to trade custom scripts for... [see profile]
 
The function is being called 2x because you have two onclick functions defined for the area that is being clicked. Just get rid of one of them and you should be fine. I'm guessing the onclick defined in the <img> tag is the one giving you problems.

-kaht

banghead.gif
 
Nope, that just makes it worse.

When you remove the onclick=&quot;&quot; from the IMG tag, and click the IMG (nested in the SPAN), the event.srcElement is the IMG!

I need the SPAN.

----------
I'm willing to trade custom scripts for... [see profile]
 
That's why I put onclick=&quot;return&quot; - to try and prevent it from firing the function.

----------
I'm willing to trade custom scripts for... [see profile]
 
if (event.srcElement.tagName=='IMG') return;

Added that to the function. Why was it not returning at the source? :(

----------
I'm willing to trade custom scripts for... [see profile]
 
You can also use:
Code:
 event.cancelBubble = true;

In your event handlers to stop the event bubbling up the document heirarchy.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

AU.gif
Check out Tek-Tips Australia New Zealand forum1155
NZ.gif
 
shouldn't you try to use return false instead of just return???
 
jtaal,

I don't think it matters. return false would return the boolean value: false, whereas return would return the value: null.

At least that's as I understood it?

Thanks for that dwarfthrower. Bit too tired to mess with it at this hour but that looks tidy :)

----------
I'm willing to trade custom scripts for... [see profile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top