-1

i tried to pop an alert when clicked on a div , didn't really succeed , i messed it a little to practice with objects.

here is the the code :

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript">
      var mine = {
        var start = {
          modeBox : function(){
            alert();
          }
        }
  }
    </script>
  </head>
  <body>
      <div id="askMode" onclick="mine.start.modeBox();">My mine</div>
    </body>
</html>

the problem : not alerting

why i don't get alert when i click on the div? live example : http://jsfiddle.net/YqP93/

1
  • Have a look at the console and you will see that you get a syntax error. Learn about the JavaScript basics and especially how to debug JavaScript code. Commented Oct 13, 2011 at 23:47

4 Answers 4

5

The following code tested in Firefox and Chrome:

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript">
      var mine = {
        start: {
          modeBox : function(){
            alert('Test');
          }
        }
  }
    </script>
  </head>
  <body>
      <div id="askMode" onclick="mine.start.modeBox();">My mine</div>
    </body>
</html>

NOTE: the code you posted in jsfiddle is not working, but you can copy and paste the code above that works in browsers mentioned.

Sign up to request clarification or add additional context in comments.

Comments

3
      this.mine = {
        start: {
          modeBox : function(){
            alert();
          }
        }

you are trying to declare a var in an object literal. I don't think this is valid js for one but more importantly you can not get at vars declared in that scope. You have to assign the property to the object. You had it right with the modeBox.

1 Comment

It appears the "var mine" is also out of scope when trying to access it from an onclick event. You need to do this.mine to bind it to the global scope. I have updated my answer to reflect this.
2

Get rid of the var deceleration (why?):

mine = {
    start: {
        modeBox: function() {
          alert();
        }
    }
};

Demo: http://jsfiddle.net/YqP93/3/

Comments

-1

try this

<!DOCTYPE html>
  <html>
  <head>
    <script type="text/javascript">
      var mine = {
        start: {
          modeBox: function(){
            alert();
          }
        }
  }
    </script>
  </head>
  <body>
      <div id="askMode" onclick="mine.start.modeBox();">My mine</div>
    </body>
</html>

1 Comment

Answer is very wrong and still got OK. This undermines the reliability of the StackOverflow!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.