Add method to Element - Javascript Object

Javascript examples for Object:prototype

Description

Add method to Element

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from   w w w .ja va  2  s  .co  m*/
var el = document.getElementsByTagName('audio')[0];
Object.defineProperty(el, 'src', {
    set: function (newSrc) {
        console.log('they set src to ' + newSrc);
    }
});
el.src = 'http://example.com/sounds.mp3';
    }

      </script> 
   </head> 
   <body> 
      <audio></audio>  
   </body>
</html>

Related Tutorials