I would like to change all the names of the attributes where class=\"testingCase\"
throughout all my whole html document.
e.g. Change:
&
One liner
$('selector').replaceWith($('selector')[0].outerHTML.replace("oldName=","newName="));
worked great for me when I needed to strip a prefix from all of my attributes
$('selector').replaceWith($('selector')[0].outerHTML.(/prefix\-/g,""));
There is no built-in method/function to "rename" an attribute in javascript, but you can create new attributes and remove other ones...
$('a.testingCase[title]').each(function() {
var $t = $(this);
$t.attr({
newTitleName: $t.attr('title')
})
.removeAttr('title');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="testingCase" href="#" title="name of testing case">Blabla</a>
<a class="testingCase" href="#" title="name of another testing case">Bloo</a>
Edit: added in the bit that makes it only select a
elements with class="testingCase"
same answer as @nickf but cleaner code:
$('a.testingCase[title]').each(function() {
var curElem = $(this);
curElem.attr("newTitleName", curElem.attr('title')).removeAttr('title');
});
It works for me!
$('input[name="descricao"]').attr('name', 'title');
I don't think you can change an attribute name but what you can do is :
<a>
tags with a title attribute;JQuery let you do that with builtin functions this way :
/* get all <a> with a "title" attribute that are testingCase
then apply an anonymous function on each of them */
$('a.testingCase[title]').each(function() {
/* create a jquery object from the <a> DOM object */
var $a_with_title = $(this);
/* add the new attribute with the title value */
$a_with_title.attr("newTitleName", $a_with_title.getAttribute('title'));
/* remove the old attribute */
$a_with_title.removeAttr('title');
});
Here's a simple jquery-style plugin that gives you a renameAttr method:
// Rename an entity attribute
jQuery.fn.renameAttr = function(oldName, newName) {
var args = arguments[0] || {};
var o = $(this[0])
o
.attr(
newName, o.attr(oldName)
)
.removeAttr(oldName)
;
};
There is also this example which adds an option to remove the data for the old attribute.