This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-01-01 18:45:01

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Javascript: Parent's Style Selector.

Hullo, everyone.
I need help doing something. So I am trying to set this image to the same height as the parent's, but it isn't working.

Here's my code:

Code:

var traket = {
/* make icon logo the size of parent height */
    getParentSize : function(id){
        var img = document.getElementById(id);
        if(img && img.style) {
            img.style.height = img.parentNode.style.height;
        }
    }
}

And in the html:

Code:

<body onload="traket.getParentSize('logo')">

It won't set it.  hmm

Offline

 

#2 2012-01-01 19:16:13

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: Javascript: Parent's Style Selector.

Mmm enums - they might be your problem, I have no experience with them but you might be doing them wrong. Couldn't you just have a normal function on its own? I know enums are all delicious and pretty but try without and you can have them later *takes the candy off the small child*

But then again it could be that the poor element with <insert id here> as an id doesnt have a parent and is returning null.

Last edited by rookwood101 (2012-01-01 19:19:09)


http://i.imgur.com/zeIZW.png

Offline

 

#3 2012-01-01 19:25:04

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Javascript: Parent's Style Selector.

rookwood101 wrote:

Mmm enums - they might be your problem, I have no experience with them but you might be doing them wrong. Couldn't you just have a normal function on its own? I know enums are all delicious and pretty but try without and you can have them later *takes the candy off the small child*

But then again it could be that the poor element with <insert id here> as an id doesnt have a parent and is returning null.

Ugh, it still doesn't work.
Hmm, I wonder what it is....

Oh, and the element does have a parent.

Offline

 

#4 2012-01-01 19:35:58

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: Javascript: Parent's Style Selector.

May we see (some) more code? As in how the rest of the page works (which element is the parent etc.)

Last edited by rookwood101 (2012-01-02 05:46:17)


http://i.imgur.com/zeIZW.png

Offline

 

#5 2012-01-02 04:14:12

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Javascript: Parent's Style Selector.

rookwood101 wrote:

May we see (some) more code? As in how the rest of the page works (which element is the parent).

Yeah, seeing the whole page would be useful.

And try your error console.  tongue

Offline

 

#6 2012-01-02 04:51:46

arch20045
New Scratcher
Registered: 2011-11-24
Posts: 7

Re: Javascript: Parent's Style Selector.

Of course the whole Code page would be usefull.......... IDK anything about it so far..Candt really tell, I would need more codes.

Offline

 

#7 2012-01-02 11:46:14

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Javascript: Parent's Style Selector.

rookwood101 wrote:

Mmm enums - they might be your problem, I have no experience with them but you might be doing them wrong. Couldn't you just have a normal function on its own? I know enums are all delicious and pretty but try without and you can have them later *takes the candy off the small child*

But then again it could be that the poor element with <insert id here> as an id doesnt have a parent and is returning null.

JavaScript doesn't have enums. JScript has them, but you shouldn't use them unless you're doing some MS-specific thing (not web development). What ProgrammingFreak is using are object literals acting as a kind of namespace, and those are good practice for not polluting the global namespace (that is, if you define many global variables, the names might clash with other scripts' globals; clashes are much less likely if you only use one global variable).

To answer the question, element.style.height will only give the correct height of an element if that element's height has been explicitly set in its style. You may be able to force the image to have display: block;, then just set its height to 100%. You could also use the offsetHeight property of the parent, which returns the current width of an element, in pixels. You have to append the unit 'px' to this value if you use it as the style.height, though, i.e.

Code:

img.style.height = img.parentNode.offsetHeight + 'px';

nXIII

Offline

 

#8 2012-01-02 15:23:44

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Javascript: Parent's Style Selector.

nXIII wrote:

rookwood101 wrote:

Mmm enums - they might be your problem, I have no experience with them but you might be doing them wrong. Couldn't you just have a normal function on its own? I know enums are all delicious and pretty but try without and you can have them later *takes the candy off the small child*

But then again it could be that the poor element with <insert id here> as an id doesnt have a parent and is returning null.

JavaScript doesn't have enums. JScript has them, but you shouldn't use them unless you're doing some MS-specific thing (not web development). What ProgrammingFreak is using are object literals acting as a kind of namespace, and those are good practice for not polluting the global namespace (that is, if you define many global variables, the names might clash with other scripts' globals; clashes are much less likely if you only use one global variable).

To answer the question, element.style.height will only give the correct height of an element if that element's height has been explicitly set in its style. You may be able to force the image to have display: block;, then just set its height to 100%. You could also use the offsetHeight property of the parent, which returns the current width of an element, in pixels. You have to append the unit 'px' to this value if you use it as the style.height, though, i.e.

Code:

img.style.height = img.parentNode.offsetHeight + 'px';

Thank you, nXIII. That works perfectly.  wink

Offline

 

#9 2012-01-02 15:51:53

flashgocrazy
Scratcher
Registered: 2011-01-12
Posts: 500+

Re: Javascript: Parent's Style Selector.

"thats enough internet, Child!" *takes computer from child*  lol
I don't know Maybe, try using Webblox to program


◕‿◕

Offline

 

#10 2012-01-02 16:03:36

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: Javascript: Parent's Style Selector.

flashgocrazy wrote:

"thats enough internet, Child!" *takes computer from child*  lol
I don't know Maybe, try using Webblox to program

He's fixed it already.


http://i.imgur.com/zeIZW.png

Offline

 

#11 2012-01-02 17:10:59

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Javascript: Parent's Style Selector.

Closed by request of the topic owner.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

Board footer