1

Hey all i am trying to get the png link from a string that houses some HTML in it.

The string looks like this:

var thecode = '[caption id="attachment_1794" align="alignleft" width="210"]
  <a href="http://www.website.com/wp-content/uploads/2012/10/Screen-Shot-2012-10-30-at-
  8.52.48-AM.png"><img class="size-medium wp-image-1794" title="Difference between and 
  affiliate programs" src="http://www.website.com/wp-content/uploads/2012/10/Screen-
  Shot-2012-10-30-at-8.52.48-AM-210x300.png" alt="Learn the and affiliate programs" 
  width="210" height="300" /></a> Learn the and affiliate programs[/caption]<p><span 
  style="font-size: small;">An article by CEO of AD, Inc. (AD®), has been published by 
  <a title="Learn the and affiliate programs" href="http://www.mmag.com/articles/85281" 
  target="_blank">M Magazine</a>. The article is titled: "Putting The Right Place." </span>';

I have been trying to get the image link via jQuery code:

var newString = $('a[href$=".png"]', thecode).attr('href');
console.log(newString);

But the example above returns back SCRIPT5022: Syntax error, unrecognized expression:

What would i be missing?

8
  • I do not have this error, which browser are you using ? Commented Feb 15, 2013 at 20:43
  • i am using IE version 9 Commented Feb 15, 2013 at 20:45
  • Are you trying to get the PDF or PNG? Commented Feb 15, 2013 at 20:45
  • What was the unrecognized expression? Commented Feb 15, 2013 at 20:45
  • 1
    Here is a JS Fiddle with the error duplicated: jsfiddle.net/X3u4H Commented Feb 15, 2013 at 20:47

1 Answer 1

6

Try this:

var thecode = '[caption id="attachment_1794" align="alignleft" width="210"] <a href="http://www.website.com/wp-content/uploads/2012/10/Screen-Shot-2012-10-30-at-8.52.48-AM.png"><img class="size-medium wp-image-1794" title="Difference between and affiliate programs" src="http://www.website.com/wp-content/uploads/2012/10/Screen-Shot-2012-10-30-at-8.52.48-AM-210x300.png" alt="Learn the and affiliate programs" width="210" height="300" /></a> Learn the and affiliate programs[/caption]<p><span style="font-size: small;">An article by CEO of AD, Inc. (AD®), has been published by <a title="Learn the and affiliate programs" href="http://www.mmag.com/articles/85281" target="_blank">M Magazine</a>. The article is titled: "Putting The Right Place." </span>';

var newString = $('<div>'+ thecode + '</div>');
console.log($('a[href$=".png"]',newString).attr('href'));

jsFiddle example

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

5 Comments

AH, so thats the reason... the [ and ]. Thanks j08691!
Why does the string need to be wrapped in a div?
@JSuar Due to how jquery parses html, it has to be wrapped in a div otherwise it won't get all of the elements. jquery doesn't handle text nodes at the top level very well.
Yeah bascially its creating a real temp div element and then only jquery can parse the html.
For example, you could avoid the error by using $.parseHTML(thecode), but you won't be able to find your target element because it doesn't properly parse the html.

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.