user3308043
user3308043

Reputation: 827

JQuery get attribute from HTML element var

I have the following Jquery code:

 $(function () {
     $("div[id$='xxx']").click(function ()
     {
         $('.greenBorder').each(function (i, obj)
         {

         });
     });
 });

When a DIV called xxx is clicked, each HTML img with a class of greenBorder is iterated through. I would like to access the src attribute of each img. I can't figure out how to pull this value out. The obj function parameter contains the HTML element object, but how do I get the value out of that object? If this were C/Java/C#, I would cast it.

Upvotes: 0

Views: 64

Answers (1)

Alexander O'Mara
Alexander O'Mara

Reputation: 60587

In the jQuery each function, you can access the current element with this. You can then jQuery select the element using $(this), and read the attribute with $(this).attr("src").

Example:

$(function () {
    $("div[id$='xxx']").click(function ()
    {
        $('.greenBorder').each(function (i, obj)
        {
            console.log($(this).attr("src"));
        });
    });
});

Alternatively, you could use obj in place of this. You could also read the src attribute with this.src or obj.src.

Upvotes: 4

Related Questions