Reputation: 5
This is kinda hard to word so i'm hoping you understand it:
I have an entire page in a variable. I need to be able to do getElementsByClassName on it. But how?
I've tried:
$.get( base_url, function( data ) {
var something = data.getElementsByClassName('.user_name');
});
Upvotes: 0
Views: 63
Reputation: 1074385
If your URL returns HTML, data
is a string. Since you're using jQuery, you can have jQuery parse it for you:
var dom = $(data);
Then you can use all the usual jQuery methods on that disconnected set of elements, so:
var userNames = dom.find(".user_name");
If you weren't using jQuery, you could have the browser parse that into elements for you:
var div = document.createElement('div');
div.innerHTML = data;
...and then use use DOM methods on that disconnected div. I wouldn't use getElementsByClassName
, though; querySelectorAll
has better support; basically, it's in all modern browsers and also in IE8, but IE8 doesn't have getElementsByClassName
.
var userNames = div.querySelectorAll(".user_name");
Upvotes: 4
Reputation: 25882
You are mixing pure javascript with JQuery
Try this
data.getElementsByClassName('user_name');
instead of
data.getElementsByClassName('.user_name');
Upvotes: 1