Use "this" reference of the element calling the function - Javascript Object

Javascript examples for Object:this

Description

Use "this" reference of the element calling the function

Demo Code

ResultView the demo in separate window


<html>
 <head> 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
 </head> /*from  www.  ja  v  a 2  s  . co  m*/
 <body>
   Click first on [Run with JS] button
  <br> 
  <br> 
  <button name="somename1">Button1</button> 
  <button name="somename2">Button2</button> 
  <script>
$('button[name^=somename]').click(function(){
   var myName = this.name;
   console.log(myName);
});

      </script>  
 </body>
</html>

Related Tutorials