user460114 user460114 - 5 months ago 15
Javascript Question

Count elements having a specified attribute

I need to count all elements having the attribute "ind", regardless of what the value of that attribute is.

<select name="region_id" class="selSumo selRelated" ind="1">


e.g. I guess and adaptation of

var cnt = jQuery(['ind']).length;


I have three different selects on the page, each with attribute "ind", but I get cnt = 1.

Answer

$("[ind]").length will give the count of all the elements with attribute ind

codepen URL for reference - http://codepen.io/nagasai/pen/RRPmZv

          $(document).ready(function(){
              alert($("[ind]").length);
          });


            <select name="region_id1" class="selSumo selRelated" ind="1">
              <select name="region_id" class="selSumo selRelated" >

                <div ind="1">1</div>

Hope this is helpful for you

Comments