0

I'm loading the html files to the div like this

var j1 = $('<div class = "cl1">').load('components/1.html');
                var j2 = $('<div class = "cl2">').load('components/2.html');
                var j3 = $('<div class = "cl3">').load('components/3.html');                


            $("#search_content").append(j1);
            $("#search_content").append(j2);
            $("#search_content").append(j3);

I want to get the count of p tag in each file. Thanks in advance.

Jegan
  • 530
  • 7
  • 27

2 Answers2

0

You can search each of the elements containing the documents for the p tag. In your given snippet:

var j1 = $('<div class = "cl1">').load('components/1.html', function() {//after content loaded
    var ptags = j1.find("p").length
});

Update: If you wish to use all three together he should use promises - see this post for loading content with promises

Community
  • 1
  • 1
megawac
  • 10,953
  • 5
  • 40
  • 61
0

so you need to count for every file? load a file, count number of paragraphs and load another, then count again? to count i thisn the simplest way is:

function count_pars(){
var n=0;
$('p').each(
function(){
n=n+1;
})
//do stuff 
}

and use like this:

$("#search_content").append(j1);
count_pars();
Catalin Sterian
  • 96
  • 1
  • 10