/    Sign up×
Community /Pin to ProfileBookmark

Controlling height with the DOM

I’ve got a question about Javascript and the DOM: is it possible to control a div’s height using it? I’ve already tried using setAttribute to no avail, and I want to know whether it is possible before I pursue it further.

Thanks in advance.

Edit: I’ve finally worked it out myself, no need to respond.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@maestrosyncauthorAug 20.2005 — Hmm... actually, my solution only works in IE, not Firefox.
var divs = document.getElementsByTagName("div");
for(var i=0; i < divs.length; i++)
{
var currentdiv = divs[i];
var currentid = currentdiv.id;
if (currentid == "textbody") {
var sdiv = currentdiv;
}
}
sdiv.style.height = 1000;
sdiv.setAttribute("height", "1000");

The loop is cumbersome and is only there because I can't get getElementsbyID or whatever it is to work. Now, sdiv.style.height works, but only in ID, and setAttribute doesn't work at all. SetAttribute seems like the best solution, but I cannot work out why it does not work.
Copy linkTweet thisAlerts:
@FangAug 20.2005 — document.getElementById('xxxx').style.cssText+= 'position:static;height:150px;';
You must use the concatenation or other styles will be deleted.

IE requires the [I]position[/I] be declared, but does not always work correctly when a [I]border[/I] has been declared.

EDIT:
var xxxxStyles=document.getElementById('xxxx').style.cssText;
document.getElementById('xxxx').style.cssText= 'height:150px;'+xxxxStyles;

This works correctly in both browsers. The Existing style is added to the added [I]height[/I].

A problem does occur, in IE, if the height has previously been declared. ?
×

Success!

Help @maestrosync spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...