How can I change HTML attribute names with jQuery?

There is no built-in method/function to “rename” an attribute in javascript, but you can create new attributes and remove other ones…

$('a.testingCaseHow can I change HTML attribute names with jQuery?').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”

Leave a Comment