0

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Would this.src, obj.src and $(obj).attr('src') work too?
@user3558931: Yes they would, I added a note of this to the answer.
Although not part of my question, how I could I get attribute values for the next element in the DOM?
I'll give you the check momentarily.
Scrap that, answered my own question with $(this).next().attr('value')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.