code_junkie

How can I change HTML attribute names with jQuery?

Nov 12th, 2011
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. $('a.testingCase[title]').each(function() {
  2. var $t = $(this);
  3. $t
  4. .attr({
  5. newTitleName : $t.attr('title'),
  6. })
  7. .removeAttr('title')
  8. ;
  9. });
  10.  
  11. /* get all <a> with a "title" attribute that are testingCase
  12. then apply an anonymous function on each of them */
  13.  
  14. $('a.testingCase[title]').each(function() {
  15.  
  16. /* create a jquery object from the <a> DOM object */
  17. var $a_with_title = $(this);
  18.  
  19. /* add the new attribute with the title value */
  20. $a_with_title.attr("newTitleName", $a_with_title.getAttribute('title'));
  21.  
  22. /* remove the old attribute */
  23. $a_with_title.removeAttr('title');
  24.  
  25. });
Add Comment
Please, Sign In to add comment