I
I
Ilya Plotnikov2011-11-03 21:20:37
JavaScript
Ilya Plotnikov, 2011-11-03 21:20:37

Jquery selector not working?

I've been suffering for half an hour, please help.
The structure is as follows: <img src="jpg"
When I click on tr, I try to get the src attribute from the image
var image = $(this).children("img").attr("src");
Tried the following:
Assigned td class image
var image = $(this).children(".image img").attr("src");
Directly
var image = $(this).children("img").attr("src");
Assigned img class image
var image = $(this).children(".image").attr("src");
var image = $(this).children("td .image").attr("src");
var image = $(this).children("td img").attr("src");

None of this works. CHDNT?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin Kitmanov, 2011-11-03
@ilyaplot

.children() is an immediate child, so $(this).children('td') will work, but $(this).children('img') won't because tr has only td as an immediate child.
Also, the argument of the children method is a filter.
Use .find(), don't miss it :)

I
Ilya Plotnikov, 2011-11-03
@ilyaplot

var image = $(this).children("td").children("img").attr("src");
Tried this right after posting the question. Earned. Horror =)

M
MT, 2011-11-13
@MTonly

the .children() method allows us to search through the immediate children of these elements
.children() – jQuery API

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question