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:
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:
<body onload="traket.getParentSize('logo')">It won't set it.
Offline
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)
Offline
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
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)
Offline
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.
Offline
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.
img.style.height = img.parentNode.offsetHeight + 'px';
Offline
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.
Offline
"thats enough internet, Child!" *takes computer from child*
I don't know Maybe, try using Webblox to program
Offline
flashgocrazy wrote:
"thats enough internet, Child!" *takes computer from child*
![]()
I don't know Maybe, try using Webblox to program
He's fixed it already.
Offline