/    Sign up×
Community /Pin to ProfileBookmark

Add Table Cell Widths…

Hello, all.

I am looking for a way to add together table cell widths for one row. So, if I add a cell to a row of one table with a specified width, that new sum can be retrieved. Any suggestions are appreciated!

Thanks,

bubbis

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@LeruraOct 18.2005 — you can add an ID to each cell

then call getElementByID('cellID').width for each cell and add this numbers together

if you want to include borders in this sum then add ([number of cells]-1)*[borderwidth]*2
Copy linkTweet thisAlerts:
@bubbisthedogauthorOct 18.2005 — Thanks for the suggestion, Lerura! That's what I'll do. I was curious though: Could you give the table an id, and then use the 'length' method in a loop to get each cell's width (as opposed to having to do a getElementById() for each cell's id)? Forgive me for being ambiguous; I'm obviously new when it comes to JavaScript.

Thanks again for the suggestion and taking the time to respond. ?

Regards,

bubbis
Copy linkTweet thisAlerts:
@LeruraOct 18.2005 — [CODE]function count(){
Numbers=0;
i=(document.getElementById('CheckRow').getElementsByTagName('td'));
for (x=0;x<i.length;x++){
Numbers+=i[x].clientWidth*1
}
document.write(Numbers)
}
[/CODE]


here I have given the Row an id [COLOR=Red]<tr ID="CheckRow">[/COLOR]
Copy linkTweet thisAlerts:
@KorOct 19.2005 — Thanks for the suggestion, Lerura! That's what I'll do. I was curious though: Could you give the table an id, and then use the 'length' method in a loop to get each cell's width (as opposed to having to do a getElementById() for each cell's id)? Forgive me for being ambiguous; I'm obviously new when it comes to JavaScript.

Thanks again for the suggestion and taking the time to respond. ?

Regards,

bubbis[/QUOTE]


[B]offsetWidth[/B] and [B]getElementsByTagName()[/B] will do the job

var myCells = document.getElementById('mytable').getElementsByTagName('td');

for(var i=0;i<myCells.length;i++){

alert('the cell no '+i+' has width= '+myCells[i].offsetWidth+' px')

}



But if you want only to know which is the width of the table after a new cell was added,simply



var tabWidth = getElementById('mytable').offsetWidth;



caution, as offsetWidth is a readonly attribute, the returned value is a [B]number[/B]!
Copy linkTweet thisAlerts:
@bubbisthedogauthorOct 19.2005 — Lerura, Kor: [B]THANK YOU[/B] . ? Both suggestions will not only prove very useful for me, but have also expanded my knowledge of how JavaScript works. Thanks again to both of you for providing your insight and taking the time to respond to my query.

Regards,

bubbis
×

Success!

Help @bubbisthedog 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 4.30,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...