Skip to main content
Home / Code Snippets / jQuery Code Snippets / Working with Attributes

Working with Attributes

Setting Single Attribute

$("img").attr("src", "/images/banner.jpg");

Setting Single Attribute (with function)

$("div").attr("id", function (arr) {
    return "div-id" + arr;
})

Setting Multiple Attributes

$("img").attr({
       src: "/images/banner.jpg",
       title: "banner",
       alt: "banner"
});

Getting Attribute

var $source = $("img").attr("src");
icon-link icon-logo-star icon-search icon-star