Problem setting attriibutes in JS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AndyG

    #1

    Problem setting attriibutes in JS

    I'm trying to mess about with a table using javascript. I'm deleting the
    current row then adding a new row and adding a couple of columns. I then
    want to add either a style attribute and add a border to the bottom of one
    of the cells or add a class attribute and set it up in the stylesheet.
    Problem is, when I try to use the setAttribute it doesn't do anything. In
    fact when I try and set any attribute of cellLeft or cellRight such as
    bgcolor=red; it does nothing. It sets the width ok though!

    Can anyone spot the problem?? Codes below..

    Cheers,
    Andy


    <SCRIPT LANGUAGE='JavaS cript1.2'>
    function setMenuComment( arg)
    {
    //remove row
    var tbl = document.getEle mentById('menuC ommentTable');
    var lastRow = tbl.rows.length ;
    if (lastRow > 0) tbl.deleteRow(l astRow - 1);

    //add row
    var lastRow = tbl.rows.length ;
    var iteration = lastRow + 1;
    var row = tbl.insertRow(l astRow);
    row.setAttribut e('align', 'left');

    // left cell
    var cellLeft = row.insertCell( 0);
    blank = document.create TextNode('blank ');
    cellLeft.append Child(blank);

    // right cell
    var cellRight = row.insertCell( 1);

    var pEl = document.create Element('p');
    if(arg=='main') { text = document.create TextNode('intro duction area');
    cellRight.setAt tribute('width' ,'750');}
    else if(arg=='cv'){ text = document.create TextNode('perso nal profile and
    information'); cellRight.setAt tribute('width' ,'600');}
    else if(arg=='downlo ads'){ text = document.create TextNode('softw are and
    documents to download'); cellRight.setAt tribute('width' ,'450');}
    else if(arg=='links' ){ text = document.create TextNode('links to other
    sites of interest'); cellRight.setAt tribute('width' ,'300');}
    pEl.appendChild (text);
    cellRight.appen dChild(pEl);
    cellRight.setAt tribute("class" , "menuInfo") ;
    }
    </SCRIPT>

    //html
    <!-- Menu Info Area -->
    <tr><td border="0">
    <table border="0" align="center" cellpadding="0"
    cellspacing="0" vspace="0" id="menuComment Table">
    <tr>
    <td class="menuBar" width="100%"
    style="border-bottom:1px solid #DCDCDC"><a>&nb sp;</a></td>
    </tr>
    </table>
    </td></tr>


    //stylesheet
    td.menuInfo
    {
    border-left:1px solid #DCDCDC;
    border-bottom:1px solid #EFEFEF;
    height: 20;
    text-align: left;
    vertical-align: middle;
    line-height: 1;
    }


  • Martin Honnen

    #2
    Re: Problem setting attriibutes in JS



    AndyG wrote:

    [color=blue]
    > cellRight.setAt tribute("class" , "menuInfo") ;[/color]

    Use
    cellRight.class Name = "menuInfo";
    setAttribute in IE is misbehaving so you are better off scripting
    properties and that way the script is working in other browsers too.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Michael Winter

      #3
      Re: Problem setting attriibutes in JS

      On Thu, 30 Sep 2004 13:47:35 +0100, AndyG <Andrew.grimble @BAAESYSTMES.CO M>
      wrote:
      [color=blue]
      > I'm trying to mess about with a table using javascript. I'm deleting the
      > current row then adding a new row and adding a couple of columns. I then
      > want to add either a style attribute and add a border to the bottom of
      > one
      > of the cells or add a class attribute and set it up in the stylesheet.
      > Problem is, when I try to use the setAttribute it doesn't do anything.[/color]

      Don't use setAttribute in HTML documents. As far as I know, all standard
      attributes have shortcut properties available. So, setting the class
      changes from:

      element.setAttr ibute('class', '...');

      to

      element.classNa me = '...';


      From:

      element.setAttr ibute('width', '8em');

      to

      element.width = '8em';


      That may, or may not, solve your problem.
      [color=blue]
      > In fact when I try and set any attribute of cellLeft or cellRight such as
      > bgcolor=red; it does nothing. It sets the width ok though![/color]

      All of those attributes are deprecated. I don't think cellLeft/Right even
      exist. If they do, they're proprietary.

      Use CSS. No align attributes. No bgcolor. Just up-to-date, structural
      mark-up.

      [snip]
      [color=blue]
      > <SCRIPT LANGUAGE='JavaS cript1.2'>[/color]

      Dear Lord! Don't EVER use JavaScript1.2 as the language specification
      unless you know what you're doing. The language rules changed in that
      version and Netscape-like browsers respect those changes.

      Stick to the type attribute. It's required anyway, and makes the language
      attribute redundant.

      <script type="text/javascript">
      [color=blue]
      > function setMenuComment( arg)
      > {
      > //remove row
      > var tbl = document.getEle mentById('menuC ommentTable');
      > var lastRow = tbl.rows.length ;
      > if (lastRow > 0) tbl.deleteRow(l astRow - 1);[/color]

      [snip]

      If this code is for the Web, read up on feature detection. Not all
      browsers in use support DOM methods and properties.

      <URL:http://jibbering.com/faq/#FAQ4_26>

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • AndyG

        #4
        Re: Problem setting attriibutes in JS

        Thanks, that worked

        "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
        news:opse45apr2 x13kvk@atlantis ...[color=blue]
        > On Thu, 30 Sep 2004 13:47:35 +0100, AndyG <Andrew.grimble @BAAESYSTMES.CO M>
        > wrote:
        >[color=green]
        > > I'm trying to mess about with a table using javascript. I'm deleting the
        > > current row then adding a new row and adding a couple of columns. I then
        > > want to add either a style attribute and add a border to the bottom of
        > > one
        > > of the cells or add a class attribute and set it up in the stylesheet.
        > > Problem is, when I try to use the setAttribute it doesn't do anything.[/color]
        >
        > Don't use setAttribute in HTML documents. As far as I know, all standard
        > attributes have shortcut properties available. So, setting the class
        > changes from:
        >
        > element.setAttr ibute('class', '...');
        >
        > to
        >
        > element.classNa me = '...';
        >
        >
        > From:
        >
        > element.setAttr ibute('width', '8em');
        >
        > to
        >
        > element.width = '8em';
        >
        >
        > That may, or may not, solve your problem.
        >[color=green]
        > > In fact when I try and set any attribute of cellLeft or cellRight such[/color][/color]
        as[color=blue][color=green]
        > > bgcolor=red; it does nothing. It sets the width ok though![/color]
        >
        > All of those attributes are deprecated. I don't think cellLeft/Right even
        > exist. If they do, they're proprietary.
        >
        > Use CSS. No align attributes. No bgcolor. Just up-to-date, structural
        > mark-up.
        >
        > [snip]
        >[color=green]
        > > <SCRIPT LANGUAGE='JavaS cript1.2'>[/color]
        >
        > Dear Lord! Don't EVER use JavaScript1.2 as the language specification
        > unless you know what you're doing. The language rules changed in that
        > version and Netscape-like browsers respect those changes.
        >
        > Stick to the type attribute. It's required anyway, and makes the language
        > attribute redundant.
        >
        > <script type="text/javascript">
        >[color=green]
        > > function setMenuComment( arg)
        > > {
        > > //remove row
        > > var tbl = document.getEle mentById('menuC ommentTable');
        > > var lastRow = tbl.rows.length ;
        > > if (lastRow > 0) tbl.deleteRow(l astRow - 1);[/color]
        >
        > [snip]
        >
        > If this code is for the Web, read up on feature detection. Not all
        > browsers in use support DOM methods and properties.
        >
        > <URL:http://jibbering.com/faq/#FAQ4_26>
        >
        > Mike
        >
        > --
        > Michael Winter
        > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


        Comment

        Working...