Reputation: 1095
This is my html:
<label id='custom'></label>
<p>test</p>
<label id='start_date[0]'></label>
<p>test</p>
<p>test</p>
<label id='start_date[1]'></label>
<label id='start_date[2]'></label>
<p>test</p>
<label id='custom2'></label>
I want to get the total number of labels, which has the id of this format:
start_date[i]
. How can I do that? Can you give some idea? Thanks.
Upvotes: 1
Views: 32
Reputation: 30557
Use the attribute contains selector like
$('[id*="start_date"]').length
If you want it to explicitly start with start_date
, use the attribute starts with ^
instead of *
$('[id^="start_date"]').length
Upvotes: 1
Reputation: 133403
You can use Attribute Starts With Selector [name^=”value”] and then get length
propert
Selects elements that have the specified attribute with a value beginning exactly with a given string.
$('label[id^="start_date"]').length
Upvotes: 1
Reputation: 337560
You can use the 'attribute begins with' selector and the length
property:
var itemCount = $('label[id^="start_date"]').length;
alert(itemCount);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id='custom'></label>
<p>test</p>
<label id='start_date[0]'></label>
<p>test</p>
<p>test</p>
<label id='start_date[1]'></label>
<label id='start_date[2]'></label>
<p>test</p>
<label id='custom2'></label>
Upvotes: 2