Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  172] [ 2]  / answers: 1 / hits: 16041  / 10 Years ago, tue, may 20, 2014, 12:00:00

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.


More From » count

 Answers
10

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;
}
});
}

[#70931] Sunday, May 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jameson

Total Points: 534
Total Questions: 103
Total Answers: 102

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
jameson questions
;