Javascript DOM How to - Get custom attribute








Question

We would like to know how to get custom attribute.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!-- w  w w  . j a  va2s  . c o m-->
var a = document.getElementById("d1").getAttribute("lname");
console.log(a);
}
</script>
</head>
<body>
  <a fname="carl" lname="monti" id="d1">..</a>
  <a fname="carl" lname="monti" id="d2">..</a>
</body>
</html>

The code above is rendered as follows: