Note that there are some explanatory texts on larger screens.

plurals
  1. POMootools class - storing reference to document body in a class property
    primarykey
    data
    text
    <p>Greetings all! I'm trying to learn Mootools classes. I've made this class to add a div to the page.</p> <pre><code>var F = new Class({ Implements: [Options, Events], options: { container: document.body, width: '250px', background: '#ccc' }, initialize: function(options) { this.setOptions(options); this.addDemoDiv(); }, addDemoDiv: function() { var dDiv = new Element('div', { 'class': 'myClass', html: 'Click me!', styles: { padding: '20px', border: '1px solid #999', width: this.options.width, background: this.options.background }, events: { click: this.animate } }); dDiv.inject(this.options.container); }, animate: function() { alert('Hello world'); } }); window.addEvent('domready', function() { var item = new F(); }); </code></pre> <p>It's supposed to allow the user to specify the container to inject the div into, with the document body being the default. When I do it like above, the code validates OK, but the script fails to add the div - Firebug and Chrome complain about the container being null or undefined.</p> <p>I have to change <code>dDiv.inject(this.options.container);</code> to this</p> <pre><code>if (!this.container) { dDiv.inject(document.body); } else { dDiv.inject(this.container); } </code></pre> <p>to make it work.</p> <p>Can any wise Mootools ninja tell me why inject works when I pass document.body in directly, but breaks when I try to pass it a reference to document.body supposedly stored in the class's container option? I've tried variations on document.body, like 'document.body' and $$('document.body') and $$(document.body).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload