Kris-I Kris-I - 7 months ago 51
jQuery Question

jQuery select with partial name

I'd like the count the element on the page with the name :

  • MyElement_1

  • MyElement_2

Then I'd like get element MyElement


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'));