/    Sign up×
Community /Pin to ProfileBookmark

assign firstChild value

I try to build a sum horizontally (for the begining) of some text fields in a table using DOM methods. Unfortunately the final asignment works only for IE. Moz does not show any error message, but does not assign the sum. Any ideeas?

[code=php]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<meta http-equiv=”Content-Style-Type” content=”text/css”>
<meta http-equiv=”Content-Script-Type” content=”text/javascript”>
<script language=”JavaScript” type=”text/JavaScript”>
function calculate(c){
d=document.getElementById(‘tab’);
totRow=0;
for(var i=0;i<d.rows.length;i++){
for(var j=0;j<d.rows[i].cells.length;j++){
if(c.parentNode==d.rows[i].cells[j]){
for (k=0;k<d.rows[i].cells.length-1;k++){
totRow=totRow+Number(d.rows[i].cells[k].firstChild.value);
}
d.rows[i].cells[d.rows[i].cells.length-1].firstChild.value=totRow;//this line works in IE only
}
}
}
}
</script>
</head>

<body>
<form>
<table border=”0″ cellpadding=”4″ id=”tab”>
<tr>
<td><input type=”text” onkeyup=”calculate(this)”></td>
<td><input type=”text” onkeyup=”calculate(this)”></td>
<td bgcolor=”#CCCCCC”>
<input type=”text” readonly=”true”></td>
</tr>
<tr>
<td><input type=”text” onkeyup=”calculate(this)”></td>
<td><input type=”text” onkeyup=”calculate(this)”></td>
<td bgcolor=”#CCCCCC”>
<input type=”text” readonly=”true”></td>
</tr>
<tr>
<td><input type=”text” onkeyup=”calculate(this)”></td>
<td><input type=”text” onkeyup=”calculate(this)”></td>
<td bgcolor=”#CCCCCC”>
<input type=”text” readonly=”true”></td>
</tr>
<tr bgcolor=”#CCCCCC”>
<td>
<input type=”text” readonly=”true”></td>
<td>
<input type=”text” readonly=”true”></td>
<td>&nbsp;</td>
</tr>
</table>

</form>
</body>
</html>
[/code]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KorauthorSep 29.2004 — I solved somehow by giving up of the total text fields (see below) but I still don't get why the previous code woun't work in Moz...
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function calculate(c){
d=document.getElementById('tab');
totRow=0;
totCol=0;
for(var i=0;i<d.rows.length;i++){
for(var j=0;j<d.rows[i].cells.length;j++){
if(c.parentNode==d.rows[i].cells[j]){
for (var k=0;k<d.rows[i].cells.length-1;k++){
totRow=totRow+Number(d.rows[i].cells[k].firstChild.value);
}
for (var m=0; m<d.rows.length-1;m++){
totCol=totCol+Number(d.rows[m].cells[j].firstChild.value);
}
d.rows[i].cells[d.rows[i].cells.length-1].firstChild.nodeValue=totRow;
d.rows[d.rows.length-1].cells[j].firstChild.nodeValue=totCol;
}
}
}
}
</script>
</head>

<body>
<form>
<table width="400" border="0" cellpadding="4" id="tab">
<tr>
<td><input type="text" onkeyup="calculate(this)"></td>
<td><input type="text" onkeyup="calculate(this)"></td>
<td width="10%" bgcolor="#CCCCCC">&nbsp; </td>
</tr>
<tr>
<td><input type="text" onkeyup="calculate(this)"></td>
<td><input type="text" onkeyup="calculate(this)"></td>
<td bgcolor="#CCCCCC">&nbsp; </td>
</tr>
<tr>
<td><input type="text" onkeyup="calculate(this)"></td>
<td><input type="text" onkeyup="calculate(this)"></td>
<td bgcolor="#CCCCCC">&nbsp; </td>
</tr>
<tr bgcolor="#CCCCCC">
<td>&nbsp; </td>
<td>&nbsp; </td>
<td>&nbsp;</td>
</tr>
</table>

</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@KorauthorSep 29.2004 — aha, someone in another Forum told me that Moz take the firstChild as the #text space of the cell, even if it is empty... What a mess...
Copy linkTweet thisAlerts:
@KorauthorSep 29.2004 — But if so, why does Moz retrieve the input's value if alert it? When alert the initial value, the <input> is the firstChild, but when assign a new value the <input> becames the"second"Child ? It makes no sense to me...

Any ideas? A Moz's bug?

Anyway, it looks like the problem can be solved even like this:

d.rows[i].cells[d.rows[i].cells.length-1].getElementsByTagName('input').value=totRow;
Copy linkTweet thisAlerts:
@FangSep 29.2004 — ? [URL=http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-F68D080]nodeValue[/URL] is read only, you appear to be attempting to give it a value.

Like this: [i]elm.firstChild.data="something";[/i]

and [i]elm.firstChild.value[/i] is, in this case, 'undefined'
Copy linkTweet thisAlerts:
@KorauthorSep 29.2004 — 
is read only, you appear to be attempting to give it a value.
[/quote]

My question was regarding my first attempt, about the firstChild.value attribute, not about the nodeValue.

Even if you say nodeValue is read only, it apperas that it [b]works[/b] even as a [b]write[/b] method. See my second thread and verify it by yourself. It works fine both for IE and MOZ. I tried nodeValue by chance, as I tried to avoid innerHTML method. Weird, but it works.
Copy linkTweet thisAlerts:
@Khalid_AliSep 29.2004 — if I rememebr correctly,in the standards, every node has a text node, so when you are manipulating it like that you will come accross this issue.First node will always be a #text node(except IE)
Copy linkTweet thisAlerts:
@KorauthorSep 30.2004 — Yes Khalid, the stadards put the #text as firstChild, but it looks like the HTML coding is important to Moz (an inconsistency behaviour, I guess)

Thus when HTML is

<td>

<input>

</td>

Moz consider , correct, #text as the firstChild, while when:

<td><input></td>

Moz consider now <input> as firstChild...

There is another difference between IE and MOZ. Nomatter the HTML coding, IE considers the firstChild.nodeValue as [b]null[/b] (which, this case, it is an IE inconsistecy behavior), while Moz says that firstChild.nodeValue is an '' empty space (which, as I said above, it is phoney as long as firstChild.value is return the <input> value).

All I want to say is that both IE and Moz give contradictory references about firstChild.
×

Success!

Help @Kor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...