Skip to content

Instantly share code, notes, and snippets.

@hcmn
Created July 28, 2012 02:53
Show Gist options
  • Save hcmn/3191518 to your computer and use it in GitHub Desktop.
Save hcmn/3191518 to your computer and use it in GitHub Desktop.
JQuery: .attr() example where callback is in action.
<!DOCTYPE html>
<!-- from JQuery API -->
<html>
<head>
<style>
div { color:blue; }
span { color:red; }
b { font-weight:bolder; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="hello">Zero-th <span></span></div>
<div>First <span></span></div>
<div>Second <span></span></div>
</body>
</html>
$(function() {
$("div").attr("id", function (arr, value) {
return "div-id" + arr + value;
})
.each(function () {
$("span", this).html("(ID = '<b>" + this.id + "</b>')");
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment