Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2012, 08:04 PM   PM User | #1
arjuns
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
arjuns is an unknown quantity at this point
Question Question regarding creating variable width table cells using javascript

I'm trying to create variable width cells within a table using javascript.
Similar to this one
http://www.protocols.com/pbook/tcpip2.htm#TCP

where width of a cell would depend on the no. of bits for a particular field.

I'm trying to assign the widths and colspans to cells using variables as follows:

document.write("<td align = 'center' width = x colspan = y>");

But I'm not able to get it to work.

It seems to work only when I hardcord a value like below:
document.write("<td align = 'center' width = 20 colspan = 3>");

Any ideas what I might be missing or how I can get this to work?

Thanks!
arjuns is offline   Reply With Quote
Old 08-28-2012, 08:48 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, first of all, you should not be using document.write(), at all. It's considered very obsolescent.

But if you must use it, you just have to do it thus:
Code:
document.write('<td align="center" width="' + x + '" colspan="' + y + '">');
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-28-2012, 10:01 PM   PM User | #3
arjuns
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
arjuns is an unknown quantity at this point
Question

Thanks!
What is the right alternative to document.write() ?
arjuns is offline   Reply With Quote
Old 08-28-2012, 10:07 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
DOM methods.

For <table>s, there are easy methods to create rows and cells.

http://www.w3schools.com/jsref/met_table_insertrow.asp
http://www.w3schools.com/jsref/met_t...insertcell.asp
and then, once you have created a cell:
http://www.w3schools.com/jsref/dom_obj_tabledata.asp

Note that align and width properties are deprecated, as that last page says. Learn to use styles, instead.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
cells, colspan, javascript, table, width

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:06 PM.


Advertisement
Log in to turn off these ads.