Count of items with having common name (different index in the middle as Name)

Home / Uncategorized / Count of items with having common name (different index in the middle as Name)

Question:
I have a record on a list and is displayed on my page. they are named like
name="detailList[0].category"
name="detailList[1].category"
name="detailList[2].category"

how would I be able to count the number of elements having this NAME. "detailList[x].category" Note: I can’t use id . because I am using jsp and it is automatically set to the name when I declared them using logic:iterate.


Answer:
If you mean detailList[x].category matches all three, i.e. x is any number

var result = [].filter.call(document.querySelectorAll(‘[name^="detailList["]’), function(item) {
return item.name.match(/^detailList[d+].category$/);
}).length;

console.log(result, ‘matches’);



Read more

Leave a Reply

Your email address will not be published. Required fields are marked *