0

Hi guys,

Can anyone please help me with this simple error. Jquery is not my strongest suite but I feel like this is something small that I'm missing. With the code below I get the error 'undefined'.

html:

<a href='#' ><img src= "./images/1.jpg" width= '200px' id="2" rel="arm1" onclick = 'test()' /></a>

Jquery:

function test(){
    alert($(this).html());
};

What I want is the HTML code of the img tag being clicked on. Is there another way to do this?

So I've tried the following and it doesn't work aswell: I've added the onclick to the ahref tag aswell.(error : undefined) I've tried passing by 'event' as a parameter. (error stating ev is not declared) when trying to get its html().

PS I do not want to use the id as a onclick function!

Anyhelp will be very much appreciated!

Thanks!

2
Contributors
3
Replies
23
Views
3 Years
Discussion Span
Last Post by Phillamon
0

Thanks for your quick response JorgeM!

I actually thought with the html() function I will be able to retrieve the <img> tag and all of it's properties with it.

Will it be possible to call the onclick via the anchor tag and then try to get it's child's html? I've tried this but wasn't able to let it work.

I'll try the attr route in a bit - thanks!

0

So after some help from friends - This worked in the end:

onclick=test(this)

jquery/js:

function test(obj){
    alert(obj.outerHTML);
};

Edited by Phillamon

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.