0
var theHTML = '<html><head><title>Hi</title><link rel="apple-icon-touch-precomposed" href="icon.jpg" /></head><body></body></html>';
alert($(theHTML).find('link[rel="apple-icon-touch-precomposed"]').attr('href'));

It alerts "undefined." I want it to return "icon.jpg". What is wrong?

1
  • 1
    Browsers are going to treat this differently. Some will strip away some of the outer elements. Commented Jul 16, 2013 at 22:28

4 Answers 4

3

Try this:

alert($(theHTML).filter('link[rel="apple-icon-touch-precomposed"]').attr('href'));

That is, use .filter() instead of .find().

Demo: http://jsfiddle.net/WmwRU/

If you do a console.log($(theHTML)) you'll see why.

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

Comments

3

You'll need to use .filter() not .find() when selecting on HTML like that.

JSFiddle

Comments

1

I don't know what you want that for, but if you use filter() instead of find() it'll work as you want:

var theHTML = '<html><head><title>Hi</title><link rel="apple-icon-touch-precomposed" href="icon.jpg" /></head><body></body></html>';
alert($(theHTML).filter('link[rel="apple-icon-touch-precomposed"]').attr('href'));

JSFiddle Demo

Comments

0

I'm not sure if you can use .find that way, I'd have to read the API about it. But, you can try .prop('href') instead of .attr('href'). If that doesn't work, I'd also suggest using an * after the = like link[rel=*"apple-icon-touch-precomposed"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.