Link to home
Start Free TrialLog in
Avatar of mychel_normandeau
mychel_normandeauFlag for Canada

asked on

onclick on an image within a td fires the tr onclick event too

I have an image in a <td>. This image have an onclick javascript event.

My problem is that the <tr> where the <td> is also have an onclick event. When I click the image, the onclick of the <tr> also fires.

How can I block the <tr> onclick when I click the <td> image ?
Avatar of raj3060
raj3060
Flag of United States of America image

Well, I don't think you can do that since image in inside td and td is inside tr. So if you are clicking on image that is inside td thus td onclick will fire too ...

Have one onclick.
mychel_normandeau,

In the onclick event in the image tag try to add "return false."  For example ...

onclick="theFunction(); return false;"

This may depend on the browser and how it handles the events but using return false will stop other default behavior after the script.

Let me know if you have any questions or need more information.

b0lsc0tt
Avatar of mychel_normandeau

ASKER

raj3060: I can't have only one click since a <tr> click put my row in "edit mode" (i change normal text to <input> with DOM) and the image click within the last <td> deletes the current row (with DOM/AJAX)...

b0lsc0tt: I tried the return false without success :(
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just out of curiousity, how does one click a TR without clicking a TD?
Badotz, are you asking me and are you serious?  The asker actually had an image tag in this and it has the onclick event but I'll be happy to talk about bubbling, etc if you are interested.  A new question would be nice though if you are really curious about this.

bol
The asker said, "My problem is that the <tr> where the <td> is also have an onclick event." Hence my question (to no one in particular) about clicking a TR. Not a word was said about an IMG tag...
Not a word by me, that is...
Badotz,

From the question body ...

"I have an image in a <td>. This image have an onclick javascript event."

Maybe I have misread it but I understood there was an image tag too and the onclick was in it.  Hopefully the asker will clarify this but it may not make a big deal for his question.

I don't mind addressing your question too but just wasn't sure if you were serious or asking me.  Also, I guess I didn't completely understand what real question was either.  If you have a question along those lines and are interested in my response then feel free to open a new question and post its URL here in a comment.  I think there is info to answer what you ask in the Quirksmode page I linked above but I'll be happy to try to add my own comments too.

bol
"how does one click a TR without clicking a TD"

TR OnClick always fires when you click anything on it's child nodes (TD, or TD's content). In my case an image within a TD had a OnClick event too and dind't wanted the TR OnClick ti fire. b0lsc0tt solution solved it.
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol