Saturday, May 9, 2015

How to get and change html attributes value - jQuery Snippet

How to get and change html attributes value - jQuery Snippet


jQuery code examples that let you understand quickly how to change single or multiple attributes values also how to get attribute value.

Changing value of Single Attribute

$("a").attr("href", "http://freetemplate.website");
$("img").attr("src", "/images/banner.jpg");

Changing value of Single Attribute with callback function

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

Changing values of multiple attributes in same html tag

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

Getting attribute value

var $source = $("img").attr("src");
Disqus Comments