How to get divs in jquery?

I used this jquery function for each function and repeated my json data with it ....

$.each(data.Results, function() {
   divs += '<div class="resultsdiv"><br />
  <span style="display: inline-block;width:150px;" class="resultName">'
 + this.Mat_Name + '</span><span class="resultfields" style="padding-left:10px;">
 Measurement&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' 
 + this.Mes_Name + '</span>&nbsp;<a href="/Materials/Delete/' + this.Id + '">
Delete</a>&nbsp;<a href="/Materials/Details/' + this.Id + '">Details</a>&nbsp;
<a href="/Materials/Edit/' + this.Id + '">Edit</a></div>';
   });

alert(divs.length); It seems that the bill does not work out .... Any suggestion ...

+3
source share
1 answer

divsis a string, so it divs.lengthwill return the length of this string. You need to convert it to a DOM node, select divs and get a counter:

$('div', '<wrapper>' + divs + '</wrapper>').length;

Alternatively, in the code you provided, you can simply get the length of the data. Array of results:

data.Results.length;
+3
source

Source: https://habr.com/ru/post/1744088/


All Articles