I'd like the count the element on the page with the name :
- MyElement_1
- MyElement_2
Then I'd like get element MyElement
Thanks,
I'd like the count the element on the page with the name :
Then I'd like get element MyElement
Thanks,
var elements = $('[name^=MyElement]');
var length = elements.length;
If you want some more advanced filtering, you can filter all the dom nodes by matching your own rules :
var elements = $('[name]').filter(function(){
return /^MyElement_\d+$/.test($(this).attr('name'));
});
Use starts with selector
:
$('[name^="MyElement"]').length
better using
$('input[name$="name"]')
To find an element by the name attribute, use:
$('[name=something]');
use *=, ^=, etc. to be more or less specific in the search term. Look at the jquery selectors page for more info on how to use them.
The exaple above returns all elements that match. Suffix this with .length and you'll get the number of elements that are found, like so:
$('[name=something]').length;