Kris-I Kris-I - 1 month ago 6
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

Thanks,

Answer
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'));
});
Comments