Adam Opalecký Adam Opalecký - 3 months ago 14x
HTML Question

jQuery: $(this).attr('id') not find id of element - undefined

I'm trying to get id and just simply alert it onclick although is does just "undefined"...
Can you help me out?
important Code:

<img src="/pictures/picture.jpg" class="r-gal-photo" id='1' onclick="alertme()" />


function alertme() {

Can you see the problem? I can't...
My bad:
I'm sorry, I wanted to simplify the code and I did not realize that I used "alert()" as name for customized function.


First problem to fix is the function name, which I suspect is different in your actual code because otherwise you wouldn't have gotten as far as you have.

You've used an old-school "onclick" attribute to associate the function with the element. Nothing assures that this will be bound to the element as you expect. You could either change the element:

Better would be to use jQuery to bind the handler:

$("#1").on("click", function() {