7

I have some html extracted to a string var, and want to then use jQuery element selection just on that string. Is this possible?

For example:

HTML:

<div class=message>
  This is a message. Click <a class=link id=link1 href=example.com>here</a>
</div>

jQuery:

$('div.message').each(function(i, item) {
  myHtml = $(this).html();
  //select <a> tag attributes here
)};

So in this example, I want to extract the id and href from the <a> tag in myHtml.

Thanks

4 Answers 4

11

If I understand correctly, you have HTML code in a string variable, and want to query within that?

// Suppose you have your HTML in a variable str
var str = '<div class="message">This is a message. Click '
        + '<a class="link" id="link1" href="example.com">here</a></div>​​​​​​​​​​​​​​​';

// You query the DOM fragment created by passing the string to jQuery function.
// $(<string>) - creates a DOM fragment (string must contain HTML)
var anchor = $('a', $(str));

// Then you can retrieve individual properties and/or contents:
var id = anchor.attr('id');
var href = anchor.attr('href');
var text = anchor.text();
Sign up to request clarification or add additional context in comments.

5 Comments

This looks promising. I will give it a try.
@Nick Craver, how is this different than my response. Other than the selector. I am trying to understand. Thanks
@durilai - Your answer selects on the DOM element (as if it exists in the current document)...that's the main difference to the question, uku wants to select elements from any string of HTML which this does by using the context argument of the selector.
@Nick Craver, thanks for the explanation. I see your what you are saying. :)
Please be aware that jQuery takes the elements inside the outermost element. In the DOM this is not a problem, as everything is wrapped in a "document" element. If your string contains multiple "sibling" elements on the top level, you need to wrap your string in a temporary tag, like a div. Consider: var str = '<h1>text</h1><p>text</p>', this would need the following selector to get the paragraph: $('p',$('<div>'+str+'</div>'));. And if you want to get the original string back, you need .parent().html(), because html() also queries inside the element!
0

You could do something like:

var aID = $('div.message a.link').attr('id');
var aHREF = $('div.message a.link').attr('href');

2 Comments

@durilai Yes, I could do that but it is not optimal in this situation.
Why? It achieves what you are looking for.
0

There is a jQuery plugin (here) that you can use to parse a string as an XML DOM and then use jQuery to query it. It needs to be XML, not the "relaxed" kind of XML that you sometimes see in HTML, though (i.e. you need quotes around your attribute values).

Comments

0

If you need to process multiple items within your structure you can do this

$('div.message').each(function(i, item) { 
  // find desendant of the item using "find"
  var alink = $(this).find('a.link');
  // Process the item
  var id = alink.attr('id'); 
  var href = alink.attr('href'); 
)}; 

1 Comment

Find method is recursive and would not use it unless you need to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.