Logo Pupungbp.com

Need a WordPress expert help?

JQuery Rename Attribute Function

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

Looking for an affordable WordPress solution?

We offer budget-friendly WordPress website development services to meet your needs. Prices start at $10.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

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