Reputation: 199
I have this code which gets the html from another page
$.get('alertjquery.html', null, function(tsv) {
alert(tsv);
});
what I want to do know is count for the number of a certain element thats in the html page.
I am a bit confused as if i did var data $('section').length;
it wouldn't count the sections in the tsv
but the sections in the current html page
this is the html page:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script>
$(document).ready(function() {
var data = $('#test').text($('section').length);
});
</script>
</head>
<body>
<section>
This should be counted
</section>
<section>
This should be counted
</section>
<section>
This should be counted
</section>
<section>
This should be counted
</section>
<section>
This should be counted
</section>
There are currently <b id='test'></b> alerts
</body>
</html>
When I alert this as tsv this also gets printed out However when I tried $tsv.find("section").length it returns 0
Upvotes: 0
Views: 92
Reputation: 4158
You can wrap the HTML received with jQuery, and then use whatever jQuery function you want on it:
$(tsv).find("section").length
EDIT:
if your tsv content isn't wrapped by <html>
or some other container (which implies it's not a "valid" html document), you can do the following:
$("<div>"+tsv+"</div>").find("section").length
Upvotes: 3
Reputation: 74420
This will work in all cases, even tsv
isn't a container:
$('<div/>', {
html: tsv
}).find('section').length;
Upvotes: 1