JQuery Rename Attribute Function

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

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:

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-‘,”) );

Here’s how to use it:
// $(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 );

Code From Wowmotty

Any idea? Fill the comment below

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More To Explore