Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all input elements with a given prefix within a div?

I have a div with id ="propertiesDiv". It has certain input elements. All of them are text fields. Some of them have names like propertyName1, propertyName2 etc. I need to get all such inputs with names starting from "propertyName".

Below is code I have written so far but it gets me all inputs within that div.

$('#propertiesDiv').find('input').each(function(index, element){
    var name = element.name;
});

Please help

like image 679
ashishjmeshram Avatar asked Oct 21 '11 10:10

ashishjmeshram


2 Answers

You're loking for jQuery's starts with selector (docs)

$('#propertiesDiv').find("input[name^='propertyName']")

Live example: http://jsfiddle.net/zZYtj/

like image 113
Jamiec Avatar answered Nov 16 '22 06:11

Jamiec


You can do like this

$( 'input[name^="propertyName"]' , $('#propertiesDiv'))
like image 40
Jayantha Lal Sirisena Avatar answered Nov 16 '22 07:11

Jayantha Lal Sirisena