Hi,

The Prototype documentation suffers from having two different uses of
the word "hash", a lower-case one ("hash") and a capitalized one
("Hash"). Where it says "hash" (lower case), it almost always means
"object" -- a plain old everyday JavaScript object, like the one in
your first example, _not_ an instance of Hash (the capitalized one).
This is because all JavaScript objects are maps (associative arrays,
unordered collections of NVPs, whatever you want to call them), which
some people call hashes (short for hashmap) even though the map may
not be a hashmap at all (it's a good way to implement a map, but not
the only way). Where the documentation says Hash (capitalized), it
means the Hash class (or an instance of it). setStyle expects an
object, not a Hash instance. You're not the only person who's been
tripped up by this.

> Or alternatively, how
> does one set the height (or perhaps any style attribute) to a value
> that comes from a calculation...

You've probably already figured this out (either from the above or in
the meantime). :-) But: Since setStyle accepts a simple object, you
just make sure the relevant property on the object is set to the
result of the calculation. You can do that with an object literal like
the one you used, because the right-hand side of a property
initializer in an object literal can be an expression, just like the
right-hand side of an assignment:

    var obj = {propName: 1 + 2 * 3};
    alert(obj.propName); // alerts "7"

So this sets the height style to 200px:

    var n = 100;
    $('elm_01').setStyle({
        height: (n * 2) + "px"
    });

The above is functionally identical to this (leaving aside the
'styles' var):

    var n = 100;
    var styles = {};
    styles.height = (n * 2) + "px";
    $('elm_01').setStyle(styles);

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com

On Jul 19, 7:54 am, Zortag <zor...@rcn.com> wrote:
> According to the Prototype documentation concerning the class method
> Element.setStyle, the method takes "a hash of property-value pairs" as
> the parameter. And, indeed, the following works as expected ...
>
>     $( 'elm_01' ).setStyle({height: '200px'});
>
> so why doesn't the following work ...
>
>     var hshElmH = $H({height: '200px'});
>     $( 'elm_01' ).setStyle ( hshElmH );
>
> The console reports no errors, the script does not error out, but the
> height of the element remains unchanged.  The variable hshElmH is set
> to an object, whose value is: height="200px".  Or alternatively, how
> does one set the height (or perhaps any style attribute) to a value
> that comes from a calculation; the Hash can be constructed from
> calculated values, how does one pass calculated values to the setStyle
> method?

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to