views:

202

answers:

3

I'm new to Javascript and Mootools and am having a bit of trouble understanding some things, especially since the documentation on Mootools is, shall we say, lacking at best.

My question is regarding the $ selector.

Why is it that I can chain some properties to it, and not others?

For example why does

$('results').style.border = "1px solid #000000";

work, but

$('results').innerHTML = "foo";

does not?

Thanks in advance.

+3  A: 

The first example is not really an example of "chaining".

style is a DOM-standard object representing the CSS style of an element. It has nothing to do with MooTools - it's just standard dot notation for accessing properties of objects.

"Chaining" is when the result of an operation on an object returns the object itself after the operation, allowing you to do stuff like this:

$('id').show().move_left(200).fadeOut();

Lastly, that second example ought to work. You should post the actual source.

Triptych
Once the Mootools methods are attached, are you no longer able to use the normal built-in Javascript functions? I thought $ also served as a shortcut for document.getElementById?
Tim76
Its a bit more complicated than that, all the different browsers have their own quirks and features. Moo has some wrapper functions for changing various attributes, fields, values which hide all the browser compat stuff from you. You can of course mix and match on the safe stuff but in general Moo very much has a style which you should try to stick with. Other post on here show examples of these helper functions like .setStyles(), .set( "html", "foo" ), .getValue(), etc. Mixing and matching though 'can' get you in a muddle, best to use the wrappers for ease (of reading and debugging)
Pete Duncanson
+1  A: 

Triptych's answer is great. I just want to help you get more moo out of mootools.

$('results').setStyle('border','1px solid #000');
$('results').set('html','foo');

// all together now
$('results').setStyle('border','1px solid #000').set('html','foo');

You don't want to use innerHTML anymore if you're grabbing elements with $ (or using any framework, really).

Functions return something when they are called. Most methods (functions) in mootools return the thing it alters (like $('results')), so you can chain another function onto it.

Your examples aren't chaining. They are simply selecting properties of your object, not calling methods.


Mootools documentation is fantastic. You're just not familiar with the language enough yet. Mootools is considered to have a steeper learning curve, so that might be part of the problem.

I was like you, new to both mootools and javascript generally. After trudging through for a while I figured mootools out, and, unknowingly, learned javascript at the same time. The docs were integral to that.

rpflo
you can use just 1 setter since 1.2 - element.set({styles: {prop:value,prop:value},html:"some string"}); and yes, you can use $ as a simple shortcut to document.getElementById. what you apply afterwards is based largely on the element prototype (to access natives or mootools functions) - but the elements returned retain whatever qualities they may have had before... i would have thought that .innerHTML is going to work as long as your browser allows it. check this: http://mooshell.net/RRVWZ . You should not access properties direct--you can't chain methods and it it may not be cross-browser.
Dimitar Christoff
A: 

http://keetology.com/blog/2009/07/00-up-the-moo-herd-mootools-tips-and-tricks

This is a great Introduction to the lower level javascript features of Mootools.

Chase