Friday, 15 April 2011

javascript - How to remove all Attributes by attribute name starts with -



javascript - How to remove all Attributes by attribute name starts with -

i remove attributes attribute name starts 'data-val' in fields has class 'read-only-state'

jquery("[data-val^='tr']" )

this give attribute 'data-val' value starts 'tr'

but need remove attributes starts 'data-val' in matched elements.

how can it?

you can utilize vanilla javascript's attributes this:

$('.read-only-state').each(function() { // native attributes object var attrs = this.attributes; var toremove = []; // cache jquery object containing element improve performance var element = $(this); // iterate attributes (attr in attrs) { if (typeof attrs[attr] === 'object' && typeof attrs[attr].name === 'string' && (/^data-val/).test(attrs[attr].name)) { // unfortunately, can not phone call removeattr straight in here, since // hurts iteration. toremove.push(attrs[attr].name); } } (var = 0; < toremove.length; i++) { element.removeattr(toremove[i]); } });

javascript jquery jquery-selectors

No comments:

Post a Comment