1

I know the following jquery syntax will set the attribute value for src

$(this).closest('img.button').attr('src', '/images/button.png');

But how can I get or grab the 'src' attribute's value for use in my jQuery script?

2 Answers 2

4

The same way, just don't provide the second parameter.

$(this).closest('img.button').attr('src');

If you provide only one parameter, .attr(...) acts as a getter.

If you provide two parameters, then .attr(...) acts as a setter.


If you want to remove the value, later, you would do:

$(this).closest('img.button').removeAttr('src');

Here is a link to the jQuery documentation for $.attr().

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

2 Comments

I have to wait 10 mins to select your answer as the answer so it will need to wait until I comeback to SO sometime -- I truly dislike that restriction in SO!
@H.Ferrence Please read the jQuery documentation first (api.jquery.com/attr). It takes less time to refer the documentation.
1

attr is getter and setter function. Docs here: http://api.jquery.com/attr/

Get attribute:

$(this).closest('img.button').attr('src');

Set attribute:

$(this).closest('img.button').attr('src', '/images/button.png');

1 Comment

@Vega and your comment does?

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.