JQuery: Count elements with similar ids

So I got stuck with the problem of counting the text fields with ids like ‘field1’, ‘field2’, ‘field3’ etc.

<input id='field1' name='somename_one'/>

<input id='field2' name='somename_two'/>

<input id='field3' name='somename_three'/>


I need to get the count of such dynamically generated fields. With JQuery, it can be done
simply with the following statement:


This statement will count the number of input fields with ids starting with ‘field’ word.

Lets say you also want to get the entered value in one of those text fields. Simple!


Also if you want the count of empty text fields with id like ‘field1’, ‘field2’, you could do it as follows:


JQuery – Shorter the better!

2 thoughts on “JQuery: Count elements with similar ids

  1. John says:

    I’m new and I understand most of what is going on here but what is the ^= operator mean. Is there a more complete list of jquery operators somewhere?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s