setAttribute() does not work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • timmy_dale12@hotmail.com

    setAttribute() does not work

    Hello , im a java programmer whos gotten tangled up in some
    javascripts. Im really stuck on this one can , can aybody explain this
    to me.

    I have a javscript which is to clone a table row and insert it below
    the current table row. this is the main part :

    // New row function. Called by form button.
    function newRow(baseRowI d, tableId) {

    // Get a reference to the base row.
    var baseRow = document.getEle mentById(baseRo wId)

    // Clone the base row.
    var newRow = baseRow.cloneNo de(true)

    // Reset the value attribute of all INPUT & SELECT elements
    // in the cloned row.
    resetRow(newRow )

    // Insert the cloned row after the last row in the table.
    document.getEle mentById(tableI d).lastChild.ap pendChild(newRo w)
    }

    function resetRow(objRef ) {


    for (var i = 0; i < objRef.childNod es.length; i++) {

    // Reset ONLY input elements (NOT checkboxes or submit/reset
    buttons)...

    if (objRef.childNo des[i].nodeName.toUpp erCase() == "INPUT" &&
    objRef.childNod es[i].getAttribute(" type").toUpperC ase() !=
    "CHECKBOX" &&
    objRef.childNod es[i].getAttribute(" type").toUpperC ase() !=
    "SUBMIT" &&
    objRef.childNod es[i].getAttribute(" type").toUpperC ase() !=
    "RESET") {

    objRef.childNod es[i].setAttribute(" value", "")

    }

    // ...and DISABLE submit buttons.

    if (objRef.childNo des[i].nodeName.toUpp erCase() == "INPUT" &&
    objRef.childNod es[i].getAttribute(" type").toUpperC ase() ==
    "SUBMIT") {
    objRef.childNod es[i].setAttribute(" disabled", "true")

    }

    // ...and rename input fileds.

    if (objRef.childNo des[i].nodeName.toUpp erCase() == "INPUT" &&
    objRef.childNod es[i].getAttribute(" name").toUpperC ase()=="CUSTOME R"){
    objRef.childNod es[i].setAttribute(" disabled", "true")
    objRef.childNod es[i].setAttribute(" value", "test")

    //THIS DOES NOT WORK
    objRef.childNod es[i].setAttribute(" name", "customer1" )

    }

    // ...and rename input fileds.

    if (objRef.childNo des[i].nodeName.toUpp erCase() == "INPUT" &&
    objRef.childNod es[i].getAttribute(" name").toUpperC ase() ==
    "FR_DATE"){
    objRef.childNod es[i].setAttribute(" disabled", "true")
    objRef.childNod es[i].setAttribute(" value", "test")

    //THIS DOES NOT WORK
    objRef.childNod es[i].setAttribute(" name", "from_date1 ")
    }

    if (objRef.childNo des[i].childNodes.len gth > 0) {
    resetRow(objRef .childNodes[i])

    }


    }

    I works ok. In the new row i can set the value of input elements,
    disable the input element but not change the name of the input
    elements. Why is this ?

    If anybody can see this i will be one happy++

    Tim
  • Fred Basset

    #2
    Re: setAttribute() does not work

    Not all setAttributes seem to work properly ... I'll leave the proper
    explanation to someone who knows what they're talking about, but you may
    find that often a workaround can be achieved by directly referencing the
    attribute you wish to set ... i.e.

    elem.name = newValue

    instead of

    elem.setAttribu te ( "name", newValue )

    Fred Basset
    fred.basset@who syourdaddy.com

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!

    Comment

    • DU

      #3
      Re: setAttribute() does not work

      timmy_dale12@ho tmail.com wrote:
      [color=blue]
      > Hello , im a java programmer whos gotten tangled up in some
      > javascripts. Im really stuck on this one can , can aybody explain this
      > to me.
      >
      > I have a javscript which is to clone a table row and insert it below
      > the current table row. this is the main part :
      >
      > // New row function. Called by form button.
      > function newRow(baseRowI d, tableId) {
      >
      > // Get a reference to the base row.
      > var baseRow = document.getEle mentById(baseRo wId)
      >
      > // Clone the base row.
      > var newRow = baseRow.cloneNo de(true)
      >[/color]

      cloneNode is not recommendable here.
      [color=blue]
      > // Reset the value attribute of all INPUT & SELECT elements
      > // in the cloned row.
      > resetRow(newRow )
      >
      > // Insert the cloned row after the last row in the table.
      > document.getEle mentById(tableI d).lastChild.ap pendChild(newRo w)
      > }
      >
      > function resetRow(objRef ) {
      >
      >
      > for (var i = 0; i < objRef.childNod es.length; i++) {
      >
      > // Reset ONLY input elements (NOT checkboxes or submit/reset
      > buttons)...
      >
      > if (objRef.childNo des[i].nodeName.toUpp erCase() == "INPUT" &&
      > objRef.childNod es[i].getAttribute(" type").toUpperC ase() !=
      > "CHECKBOX" &&
      > objRef.childNod es[i].getAttribute(" type").toUpperC ase() !=
      > "SUBMIT" &&
      > objRef.childNod es[i].getAttribute(" type").toUpperC ase() !=
      > "RESET") {
      >
      > objRef.childNod es[i].setAttribute(" value", "")
      >[/color]

      objRef.childNod es[i].value = "";


      setAttribute should be use only if it's not possible to set the
      attribute value directly.

      [color=blue]
      > }
      >
      > // ...and DISABLE submit buttons.
      >
      > if (objRef.childNo des[i].nodeName.toUpp erCase() == "INPUT" &&
      > objRef.childNod es[i].getAttribute(" type").toUpperC ase() ==
      > "SUBMIT") {
      > objRef.childNod es[i].setAttribute(" disabled", "true")
      >[/color]

      objRef.childNod es[i].disabled = true;

      [color=blue]
      > }
      >
      > // ...and rename input fileds.
      >
      > if (objRef.childNo des[i].nodeName.toUpp erCase() == "INPUT" &&
      > objRef.childNod es[i].getAttribute(" name").toUpperC ase()=="CUSTOME R")[/color]

      objRef.childNod es[i].name.toUpperCa se() == "CUSTOMER"

      {[color=blue]
      > objRef.childNod es[i].setAttribute(" disabled", "true")[/color]

      Same here.
      [color=blue]
      > objRef.childNod es[i].setAttribute(" value", "test")[/color]

      Same here.
      [color=blue]
      >
      > //THIS DOES NOT WORK
      > objRef.childNod es[i].setAttribute(" name", "customer1" )
      >[/color]

      objRef.childNod es[i].name = "customer1" ;
      [color=blue]
      > }
      >
      > // ...and rename input fileds.
      >
      > if (objRef.childNo des[i].nodeName.toUpp erCase() == "INPUT" &&
      > objRef.childNod es[i].getAttribute(" name").toUpperC ase() ==
      > "FR_DATE"){
      > objRef.childNod es[i].setAttribute(" disabled", "true")
      > objRef.childNod es[i].setAttribute(" value", "test")
      >
      > //THIS DOES NOT WORK
      > objRef.childNod es[i].setAttribute(" name", "from_date1 ")
      > }
      >
      > if (objRef.childNo des[i].childNodes.len gth > 0) {
      > resetRow(objRef .childNodes[i])
      >
      > }
      >
      >
      > }
      >
      > I works ok. In the new row i can set the value of input elements,
      > disable the input element but not change the name of the input
      > elements. Why is this ?
      >
      > If anybody can see this i will be one happy++
      >
      > Tim[/color]


      Your whole code is suspicious, IMO.

      DU
      --
      Javascript and Browser bugs:

      - Resources, help and tips for Netscape 7.x users and Composer
      - Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x


      Comment

      • timmy_dale12@hotmail.com

        #4
        Re: setAttribute() does not work

        > cloneNode is not recommendable here.

        what is recommendable then ?
        I have though about a function that will paste a new row but, took the
        easy way out with clondeNode
        [color=blue]
        > Your whole code is suspicious, IMO.[/color]

        Well i havent worked alot with javascript so im just trying to keep my
        head above water here.

        By the way is there a good web tutorial that dosent focus to much on
        the basics. I have worked alot with Java and know the basic stuff , i
        would however try to learn a bit more about the structure of
        javascript.

        Comment

        Working...