最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Count elements by id - Stack Overflow

programmeradmin7浏览0评论

I need to be able to set a var with the number of open orders on a website I use.

Warning: This site, for some reason, uses the same ids over and over again per order, but as this is not my site, I can not change that.

code of my current orderlist:

<div id="orderlist">
<label id="l61">05/19/2014 08:57:34</label>
<label id="l62">0.00066000</label>
<label id="l63">Buy</label>
<label id="l64">6</label>
<label id="l64">6</label>
<label id="l62">0.00396000</label>
<label id="l65" onclick="cancelOrder(346046);">Cancel</label>
<div id="b1"></div>
<label id="l61">05/19/2014 03:08:35</label>
<label id="l62">0.00078000</label>
<label id="l63">Sell</label>
<label id="l64">2</label>
<label id="l64">1</label>
<label id="l62">0.00078000</label>
<label id="l65" onclick="cancelOrder(38493);">Cancel</label>
<div id="b1"></div>
<label id="l61">05/19/2014 03:12:08</label>
<label id="l62">0.00076000</label>
<label id="l63">Sell</label>
<label id="l64">14</label>
<label id="l64">14</label>
<label id="l62">0.01064000</label>
<label id="l65" onclick="cancelOrder(38495);">Cancel</label>
<div id="b1"></div>
<label id="l61">05/19/2014 03:13:49</label>
<label id="l62">0.00077000</label>
<label id="l63">Sell</label>
<label id="l64">15</label>
<label id="l64">15</label>
<label id="l62">0.01155000</label>
<label id="l65" onclick="cancelOrder(38497);">Cancel</label>
<div id="b1"></div> </div>
</div>

What I'd need to do basically, is count the number of ids "l65". Or another id, it doesn't matter, they are all used each time a new order is added.

What would be the easiest way to do this? I'd need this number in a var, so that I can perform functions based on that number.

I need to be able to set a var with the number of open orders on a website I use.

Warning: This site, for some reason, uses the same ids over and over again per order, but as this is not my site, I can not change that.

code of my current orderlist:

<div id="orderlist">
<label id="l61">05/19/2014 08:57:34</label>
<label id="l62">0.00066000</label>
<label id="l63">Buy</label>
<label id="l64">6</label>
<label id="l64">6</label>
<label id="l62">0.00396000</label>
<label id="l65" onclick="cancelOrder(346046);">Cancel</label>
<div id="b1"></div>
<label id="l61">05/19/2014 03:08:35</label>
<label id="l62">0.00078000</label>
<label id="l63">Sell</label>
<label id="l64">2</label>
<label id="l64">1</label>
<label id="l62">0.00078000</label>
<label id="l65" onclick="cancelOrder(38493);">Cancel</label>
<div id="b1"></div>
<label id="l61">05/19/2014 03:12:08</label>
<label id="l62">0.00076000</label>
<label id="l63">Sell</label>
<label id="l64">14</label>
<label id="l64">14</label>
<label id="l62">0.01064000</label>
<label id="l65" onclick="cancelOrder(38495);">Cancel</label>
<div id="b1"></div>
<label id="l61">05/19/2014 03:13:49</label>
<label id="l62">0.00077000</label>
<label id="l63">Sell</label>
<label id="l64">15</label>
<label id="l64">15</label>
<label id="l62">0.01155000</label>
<label id="l65" onclick="cancelOrder(38497);">Cancel</label>
<div id="b1"></div> </div>
</div>

What I'd need to do basically, is count the number of ids "l65". Or another id, it doesn't matter, they are all used each time a new order is added.

What would be the easiest way to do this? I'd need this number in a var, so that I can perform functions based on that number.

Share Improve this question asked May 20, 2014 at 15:19 WannesWannes 831 gold badge3 silver badges12 bronze badges 4
  • 3 An HTML id MUST be unique. Use a class if you want to keep the current structure – Sterling Archer Commented May 20, 2014 at 15:20
  • 2 In my experience, using same id more than once in a html document, is asking for trouble. – Farid Nouri Neshat Commented May 20, 2014 at 15:21
  • As stated, I do not run that site, I just want to count how many orders I have on that site. I can not change anything on their end... – Wannes Commented May 20, 2014 at 15:22
  • $("label[id^=l65]").length will do the trick, you just need to pass the id – mohamedrias Commented May 20, 2014 at 15:28
Add a ment  | 

3 Answers 3

Reset to default 3

This is how you can do this It will not match exect id but the id starts with l61

 $('[id^=l61]').length

Simplest trick :

   $("label[id^=l65]").length  // will directly give you the count

Traditional :

 $("label").each(function() {
       var totalId = countId(this.id);
       console.log(this.id + " : "+totalId); // this will alert l65 : count of 165
    });

Reusable function: pass id which is 165

function countId(id) {
   var counter = 0;
   $("label").each(function() {
     if(this.id==id) {
        counter+=1;
      } 
   });
}

Try this:

html:

<div id="orderlist">
<label id="l61">05/19/2014 08:57:34</label>
<label id="l62">0.00066000</label>
<label id="l63">Buy</label>
<label id="l64">6</label>
<label id="l64">6</label>
<label id="l62">0.00396000</label>
<label id="l65" onclick="cancelOrder(346046);">Cancel</label>
<div id="b1"></div>
<label id="l61">05/19/2014 03:08:35</label>
<label id="l62">0.00078000</label>
<label id="l63">Sell</label>
<label id="l64">2</label>
<label id="l64">1</label>
<label id="l62">0.00078000</label>
<label id="l65" onclick="cancelOrder(38493);">Cancel</label>
<div id="b1"></div>
<label id="l61">05/19/2014 03:12:08</label>
<label id="l62">0.00076000</label>
<label id="l63">Sell</label>
<label id="l64">14</label>
<label id="l64">14</label>
<label id="l62">0.01064000</label>
<label id="l65" onclick="cancelOrder(38495);">Cancel</label>
<div id="b1"></div>
<label id="l61">05/19/2014 03:13:49</label>
<label id="l62">0.00077000</label>
<label id="l63">Sell</label>
<label id="l64">15</label>
<label id="l64">15</label>
<label id="l62">0.01155000</label>
<label id="l65" onclick="cancelOrder(38497);">Cancel</label>
<div id="b1"></div> </div>
</div>

js:

$(function(){
    var i=0;
    $("label").each(function(){
        if($(this).attr("id") == "l65")
          i++;
    });
    console.log(i);
});

fiddle

This example counts all labels which id's are equal to lbl65.

发布评论

评论列表(0)

  1. 暂无评论