access an object through its name as a string - Javascript Object

Javascript examples for Object:Object Method

Description

access an object through its name as a string

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
            var door = {"opened":false, "window":{"opened":false} };
            function knocking(event){
               var knockedThing = event.target.id;
               console.log(knockedThing);
               console.log("door is opened : " + window[knockedThing].opened);
               console.log("door window is opened : " + window[knockedThing].window.opened);
            }// w  w w.  ja v a  2  s  .  co m
        
      </script> 
      <button id="door" onclick="knocking(event)">A door</button>  
   </body>
</html>

Related Tutorials