Working with Attributes


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Setting Single Attribute
  2. $("img").attr("src", "/images/banner.jpg");
  3.  
  4.  
  5. Setting Single Attribute (with function)
  6. $("div").attr("id", function (arr) {
  7. return "div-id" + arr;
  8. })
  9.  
  10.  
  11. Setting Multiple Attributes
  12. $("img").attr({
  13. src: "/images/banner.jpg",
  14. title: "banner",
  15. alt: "banner"
  16. });
  17.  
  18.  
  19. Getting Attribute
  20. var $source = $("img").attr("src");

URL: http://css-tricks.com/snippets/jquery/working-with-attributes/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.