How to remove all attributes by attribute name begins with

I would like to remove all attributes whose attribute names begin with "data-val" in fields that have a read-only class

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

This will give the value of the 'data-val' attribute, which starts with 'tr'

But I need to remove all attributes that start with 'data-val' in the matched elements.

How can i do this?

0
source share
1 answer

You can use vanilla javascript attributes for this:

 $('.read-only-state').each(function() { // get the native attributes object var attrs = this.attributes; var toRemove = []; // cache the jquery object containing the element for better performance var element = $(this); // iterate the attributes for (attr in attrs) { if (typeof attrs[attr] === 'object' && typeof attrs[attr].name === 'string' && (/^data-val/).test(attrs[attr].name)) { // Unfortunately, we can not call removeAttr directly in here, since it // hurts the iteration. toRemove.push(attrs[attr].name); } } for (var i = 0; i < toRemove.length; i++) { element.removeAttr(toRemove[i]); } }); 
+6
source

All Articles