Kris-I Kris-I - 1 year ago 105
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


Answer Source
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'));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download