setAttribute() does not work

T

timmy_dale12

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(baseRowId, tableId) {

// Get a reference to the base row.
var baseRow = document.getElementById(baseRowId)

// Clone the base row.
var newRow = baseRow.cloneNode(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.getElementById(tableId).lastChild.appendChild(newRow)
}

function resetRow(objRef) {


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

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

if (objRef.childNodes.nodeName.toUpperCase() == "INPUT" &&
objRef.childNodes.getAttribute("type").toUpperCase() !=
"CHECKBOX" &&
objRef.childNodes.getAttribute("type").toUpperCase() !=
"SUBMIT" &&
objRef.childNodes.getAttribute("type").toUpperCase() !=
"RESET") {

objRef.childNodes.setAttribute("value", "")

}

// ...and DISABLE submit buttons.

if (objRef.childNodes.nodeName.toUpperCase() == "INPUT" &&
objRef.childNodes.getAttribute("type").toUpperCase() ==
"SUBMIT") {
objRef.childNodes.setAttribute("disabled", "true")

}

// ...and rename input fileds.

if (objRef.childNodes.nodeName.toUpperCase() == "INPUT" &&
objRef.childNodes.getAttribute("name").toUpperCase()=="CUSTOMER"){
objRef.childNodes.setAttribute("disabled", "true")
objRef.childNodes.setAttribute("value", "test")

//THIS DOES NOT WORK
objRef.childNodes.setAttribute("name", "customer1")

}

// ...and rename input fileds.

if (objRef.childNodes.nodeName.toUpperCase() == "INPUT" &&
objRef.childNodes.getAttribute("name").toUpperCase() ==
"FR_DATE"){
objRef.childNodes.setAttribute("disabled", "true")
objRef.childNodes.setAttribute("value", "test")

//THIS DOES NOT WORK
objRef.childNodes.setAttribute("name", "from_date1")
}

if (objRef.childNodes.childNodes.length > 0) {
resetRow(objRef.childNodes)

}


}

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
 
F

Fred Basset

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.setAttribute ( "name", newValue )

Fred Basset
(e-mail address removed)
 
D

DU

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(baseRowId, tableId) {

// Get a reference to the base row.
var baseRow = document.getElementById(baseRowId)

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

cloneNode is not recommendable here.
// 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.getElementById(tableId).lastChild.appendChild(newRow)
}

function resetRow(objRef) {


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

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

if (objRef.childNodes.nodeName.toUpperCase() == "INPUT" &&
objRef.childNodes.getAttribute("type").toUpperCase() !=
"CHECKBOX" &&
objRef.childNodes.getAttribute("type").toUpperCase() !=
"SUBMIT" &&
objRef.childNodes.getAttribute("type").toUpperCase() !=
"RESET") {

objRef.childNodes.setAttribute("value", "")


objRef.childNodes.value = "";
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-49531485

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

}

// ...and DISABLE submit buttons.

if (objRef.childNodes.nodeName.toUpperCase() == "INPUT" &&
objRef.childNodes.getAttribute("type").toUpperCase() ==
"SUBMIT") {
objRef.childNodes.setAttribute("disabled", "true")


objRef.childNodes.disabled = true;
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-50886781
}

// ...and rename input fileds.

if (objRef.childNodes.nodeName.toUpperCase() == "INPUT" &&
objRef.childNodes.getAttribute("name").toUpperCase()=="CUSTOMER")


objRef.childNodes.name.toUpperCase() == "CUSTOMER"

{
objRef.childNodes.setAttribute("disabled", "true")


Same here.
objRef.childNodes.setAttribute("value", "test")


Same here.
//THIS DOES NOT WORK
objRef.childNodes.setAttribute("name", "customer1")


objRef.childNodes.name = "customer1";
}

// ...and rename input fileds.

if (objRef.childNodes.nodeName.toUpperCase() == "INPUT" &&
objRef.childNodes.getAttribute("name").toUpperCase() ==
"FR_DATE"){
objRef.childNodes.setAttribute("disabled", "true")
objRef.childNodes.setAttribute("value", "test")

//THIS DOES NOT WORK
objRef.childNodes.setAttribute("name", "from_date1")
}

if (objRef.childNodes.childNodes.length > 0) {
resetRow(objRef.childNodes)

}


}

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



Your whole code is suspicious, IMO.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
T

timmy_dale12

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
Your whole code is suspicious, IMO.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top