Calling a jQuery method named in variable - Javascript jQuery

Javascript examples for jQuery:Variable

Description

Calling a jQuery method named in variable

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){// ww w  .  ja  v  a2 s. co  m
var method = 'attr("src")';
console.log((new Function( "return $('img')."+method )()));
    });

      </script> 
   </head> 
   <body> 
      <img src="test.png">  
   </body>
</html>

Related Tutorials