Pages

Feb 18, 2016

how to set And get Custom Attribute Value Using Jquery



How to set and get custom attribute value using jquery



An easy way to set and get custom attribute for any element using jquery.Here  i have set custom attribute for <a> tag and named it “data-emailid”.


Example


<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
</head>
<body>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”></script>
<script>
jQuery(document).ready(function(){
jQuery(‘.alerter’).click(function(){
var emailid = jQuery(this).attr(‘data-emailid’);
alert(emailid);
});
});
</script>
<a href=”#” class=”alerter” data-emailid=”test@mail.com”>Alert Mail ID</a>
</body>
</html>

No comments: