Last Updated: April 22, 2014 | Reading Time: < 1 minute
JQuery currently not support ability for Attribute renaming. Well, I’m not quite handy with Javascript, so I searched the net, and found a generous people sharing his code:
[javascript]
jQuery.fn.extend({
renameAttr: function( name, newName, removeData ) {
var val;
return this.each(function() {
val = jQuery.attr( this, name );
jQuery.attr( this, newName, val );
jQuery.removeAttr( this, name );
// remove original data
if (removeData !== false){
jQuery.removeData( this, name.replace(‘data-‘,”) );
}
});
}
});
[/javascript]
Here’s how to use it:
[js]
// $(selector).renameAttr(original-attr, new-attr, removeData);
// removeData flag is true by default
$(‘#test’).renameAttr(‘data-test’, ‘data-new’ );
// removeData flag set to false will not remove the
// .data("test") value
$(‘#test’).renameAttr(‘data-test’, ‘data-new’, false );
[/js]
Code From Wowmotty