0

I'm getting some html from a angular get request and I'm trying to get some specific elements from this response. Which is a list of div's with the class "post-holder". Extracting the html works fine, but to extract each div to insert separately has some troubles.

This is the code I'm trying with:

var firstTag = '<main id="posts">';
var res = data.substring(data.indexOf(firstTag) + firstTag.length, data.indexOf('</main>'));
var html2 = $.parseHTML( res );
var x = html2.getElementsByClassName("post-holder");

The last line gives me the following error in chrome: "TypeError: undefined is not a function".

I'm guessing that getElementsByClass has some troubles with the variable generated by jquery. Is there another approach to do the same or a way to fix what I already have?

Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
just_user
  • 11,769
  • 19
  • 90
  • 135

1 Answers1

2

See the documentation for parseHTML:

Returns: Array

Description: Parses a string into an array of DOM nodes.

getElementsByClassName is a method on DOM elements, not on arrays. You need to loop over the array, check if each value is an element (as opposed to, for instance, a text node or a comment) and then call getElementsByClassName on the values.

Or you could just construct a jQuery object and use .find() on it instead.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335