1

I have

$(elements[current]).html(); 

that returns

<a href="#"><img style="width:991px;height:230px;" src="img/image.jpg" alt="img"></a>

and i need to get the element a href value only. how can i do this?

3
  • do you mean like this api.jquery.com/attr Commented Dec 30, 2011 at 15:01
  • 2
    Do spend some time on references Commented Dec 30, 2011 at 15:07
  • 1
    I don't think this question deserves negative vote. There are always people that have questions that others do know but others don't. That's the nature of the question. I am a newbie in jQuery, i am now scared to ask questions here :S Commented Dec 30, 2011 at 16:50

6 Answers 6

3

I assume elements[current] refers to the element that is the parent of your <a etc...><img /></a> set of elements?

If so, then the answer should:

$(elements[current]).children('a').attr('href');
Sign up to request clarification or add additional context in comments.

Comments

2

Use .attr.

$(elements[current]).attr('href')

Comments

1

$(elements[current]).find('a').attr('href')

5 Comments

That won't work because $(elements[current]) is the a tag, so $(elements[current]).find('a') will not work.
@jpic judging from the html(), the a is a child node of elements[current]
You could also do $("a", "elements[current]").attr('href'). It does the same thing, without the need to use .find :)
@ShadowScripter Except not as you have quotes around elements[current] which will make it not work.
@AdamTerlson Whops, didn't see that it was an element, I was thinking it was a class or something. It is valid without the quotes though. :P
1

There is .attr() method in jquery that allow you to get and set the attribute of the html element.

Example : $("img").attr("alt");

Comments

1
$(elements[current]).find('a').attr('href');

Comments

1
elements[current].firstChild.href

Comments

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.