Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Thanks!
    What is the right alternative to document.write() ?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •