Get and set tag attribute via Element object - Javascript DOM

Javascript examples for DOM:Element getAttribute

Description

Get and set tag attribute via Element object

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>MySite</title> 
   </head> 
   <body> 
      <div id="mydiv" testarg="teststring"></div> 
      <script>
    var element = document.getElementById("mydiv");
    var argstring = element.getAttribute('testarg');
    console.log(argstring);
    element.setAttribute('testarg', 'changed');
    console.log(element.getAttribute('testarg'));
  
      </script>  
   </body>//from  w w  w. j av a 2 s . co m
</html>

Related Tutorials