1

I have a asp.net button that render as,

<button onclick="Person.Eat.rice();">Click me</button>

I am using Object Literal Pattern here,

var Person = Person || {};

Person.Eat = {
    rice: function() {
        console.log("Eating Rice");
    },
};

Error

Uncaught ReferenceError: Person is not defined

Question

Is there anyway I can use Object Literal Pattern in ASP.Net Button's onclick event like above ?

Fiddle

https://jsfiddle.net/ap8dn4nr/

Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • 1
    You need to declare your JS code in the right place - https://jsfiddle.net/ap8dn4nr/1/. Note in the bar on the left, I placed your JS code in the `` of the page. Putting it before the `` would work too. – Rory McCrossan Oct 20 '15 at 08:50
  • 1
    Works after setting option to `no wrap`. Check [updated fiddle](https://jsfiddle.net/tusharj/fyhbjgyv/). For reference on how it works see [fiddle docs](http://doc.jsfiddle.net/basic/introduction.html#fiddle-settings-sidebar) or http://stackoverflow.com/a/31848917/2025923 or http://stackoverflow.com/a/32925927/2025923 – Tushar Oct 20 '15 at 08:50

1 Answers1

1

You need to declare the script before using them in inline attribute event.

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125