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!

There is no HTML property for that element in this scenario. What you probably mean is you want the value of the ref attribute.

Use attr()

http://api.jquery.com/attr/

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!

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

onclick=test(this)

jquery/js:

function test(obj){
    alert(obj.outerHTML);
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.