473,568 Members | 2,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript onChange event does not work in IE

The following basic script works fine in firefox by not in IE. Can anyone
spot the problem? In IE I can only delete the first line but not the lines
created by javascript. Also, look at the HTML code for the first line
(click the Table HTML button:)) you will fine that the code displayed is not
the same as
was written. "onChange" was changed to "onchange" etc. Please help.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator " content="PSPad editor, www.pspad.com">
<title>testin g tables</title>
<script type="text/javascript">
<!-- Begin Add Row to Itemlist Table
function addRowToTable() {
var tbl = document.getEle mentById('iteml ist');
var lastRow = tbl.rows.length ;
var row = tbl.insertRow(l astRow);

// qty cell
var qty2 = row.insertCell( 0);
var el_qty = document.create Element('input' );
el_qty.setAttri bute('type', 'text');
el_qty.setAttri bute('name', 'qty');
el_qty.setAttri bute('size', '7');
el_qty.setAttri bute('maxlength ', '7');
el_qty.setAttri bute('onChange' , 'calc_total()') ;
qty2.appendChil d(el_qty);

// price cell
var price2 = row.insertCell( 1);
var el_price = document.create Element('input' );
el_price.setAtt ribute('type', 'text');
el_price.setAtt ribute('name', 'cost');
el_price.setAtt ribute('size', '7');
el_price.setAtt ribute('maxleng th', '7');
el_price.setAtt ribute('onChang e', 'calc_total()') ;
price2.appendCh ild(el_price);

// remove me cell
var D = row.insertCell( 2);
//D.innerHTML='<a href="javascrip t:removeRowFrom Table(this)">-</a>'
D.setAttribute( 'onClick','remo veRowFromTable( this)');
var el_txt = document.create TextNode("-");
D.appendChild(e l_txt);
}
//
function getRowIndex (cell) {
return document.all ? cell.parentElem ent.rowIndex :
cell.parentNode .rowIndex;
}

//Remove a row from the table
function removeRowFromTa ble(cell){
//alert ("start function");
var tbl = document.getEle mentById('iteml ist');
var lastRow = tbl.rows.length ;
//alert (lastRow);
var rowToDelete = getRowIndex(cel l);
//alert (rowToDelete);
if (lastRow > 2) tbl.deleteRow(r owToDelete);
}

// Calculate the total
function calc_total() {
//Do Nothing yet
}
function showtableinnerh tml(){
alert(document. getElementById( 'itemlist').inn erHTML);
}
//-->
</script>
</head>
<body>
<form name="myform" method="post" action="myform. php">
<table id="itemlist" border="1">
<tr><th>Qty</th><th>Cost</th><th>-</th></tr>
<tr>
<td>
<input type="text" name="qty" size="7" maxlength="7"
onChange="calc_ total()">
</td>
<td>
<input type="text" name="cost" size="7" maxlength="7"
onChange="calc_ total()">
</td>
<td onClick="remove RowFromTable(th is)">-</td>
</tr>
</table>
<hr>
<table id="totalcost" border="1">
<tr>
<td>
<input type="text" size="7" name="test" readonly>
</td>
<td>
<input type="text" size="7" name="total" readonly>
</td>
</tr>
</table>
<input type="button" value="TableHTM L" onclick="showta bleinnerhtml()" >
<input type="button" value="Add" onclick="addRow ToTable();">
</form>
</body>
</html>
Jul 23 '05 #1
4 11935
"rick" <rmsingh@no_spa m_rogers.com> writes:
The following basic script works fine in firefox by not in IE. Can anyone
spot the problem?
A guess:
function addRowToTable() {
var tbl = document.getEle mentById('iteml ist');
var lastRow = tbl.rows.length ;
var row = tbl.insertRow(l astRow);

// qty cell
var qty2 = row.insertCell( 0);
var el_qty = document.create Element('input' );
el_qty.setAttri bute('type', 'text');
IE doesn't translate values set with setAttribute to operative values.
To make it work in IE, you should write
el_qty.type = "text";
It should also work for modern browsers, as long as the property is
part of the W3C DOM 2 HTML specification.

My guess at what goes wrong is that ...
D.setAttribute( 'onClick','remo veRowFromTable( this)');
... it will definitly not recognize this one. Use this instead:
---
D.onclick = function(){remo veRowFromTable( this);};
---
function getRowIndex (cell) {
return document.all ? cell.parentElem ent.rowIndex :
cell.parentNode .rowIndex;


You are using the existence of document.all to decide whether to use
cell.parentElem ent or cell.parentNode . What about non-IE browsers that
has document.all but not cell.perentElem ent (I don't know if there are
any, but I don't know that there isn't either).

Just test for the property you need, with a fallback if it isn't there:
---
function getRowIndex(cel l) {
return (cell.parentNod e || cell.parentElem ent).rowIndex;
}
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:oe******** **@hotpop.com.. .
IE doesn't translate values set with setAttribute to operative values.
To make it work in IE, you should write
el_qty.type = "text";
It should also work for modern browsers, as long as the property is
part of the W3C DOM 2 HTML specification. I tried it but it does not work....but no error is generated.
Your suggested code does not display when I hit the 'TableHTML' button
so I think the properties is not being assigned.
My guess at what goes wrong is that ...
D.setAttribute( 'onClick','remo veRowFromTable( this)');


.. it will definitly not recognize this one. Use this instead:
---
D.onclick = function(){remo veRowFromTable( this);};

This does not work either same reason I think? below is the
modified sample that tried.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator " content="PSPad editor, www.pspad.com">
<title>testin g tables</title>
<script type="text/javascript">
<!-- Begin Add Row to Itemlist Table
function addRowToTable() {
var tbl = document.getEle mentById('iteml ist');
var lastRow = tbl.rows.length ;
var row = tbl.insertRow(l astRow);

// qty cell
var qty2 = row.insertCell( 0);
var el_qty = document.create Element('input' );
el_qty.type = "text";
el_qty.setAttri bute('name', 'qty');
el_qty.setAttri bute('size', '7');
el_qty.setAttri bute('maxlength ', '7');
el_qty.onChange = function(){calc _total();};
qty2.appendChil d(el_qty);

// price cell
var price2 = row.insertCell( 1);
var el_price = document.create Element('input' );
el_price.setAtt ribute('type', 'text');
el_price.setAtt ribute('name', 'cost');
el_price.setAtt ribute('size', '7');
el_price.setAtt ribute('maxleng th', '7');
el_price.setAtt ribute('onChang e', 'calc_total()') ;
price2.appendCh ild(el_price);

// remove me cell
var D = row.insertCell( 2);
//D.innerHTML='<a href="javascrip t:removeRowFrom Table(this)">-</a>'
D.onClick = function(){remo veRowFromTable( this);};
var el_txt = document.create TextNode("-");
D.appendChild(e l_txt);
}
//
function getRowIndex (cell) {
return (cell.parentEle ment || cell.parentNode ).rowIndex;
}

//Remove a row from the table
function removeRowFromTa ble(cell){
alert ("start function");
var tbl = document.getEle mentById('iteml ist');
var lastRow = tbl.rows.length ;
//alert (lastRow);
var rowToDelete = getRowIndex(cel l);
//alert (rowToDelete);
if (lastRow > 2) tbl.deleteRow(r owToDelete);
}

// Calculate the total
function calc_total() {
//Do Nothing yet
}
function showtableinnerh tml(){
alert(document. getElementById( 'itemlist').inn erHTML);
}
//-->
</script>
</head>
<body>
<form name="myform" method="post" action="myform. php">
<table id="itemlist" border="1">
<tr><th>Qty</th><th>Cost</th><th>-</th></tr>
<tr>
<td>
<input type="text" name="qty" size="7" maxlength="7"
onChange="calc_ total()">
</td>
<td>
<input type="text" name="cost" size="7" maxlength="7"
onChange="calc_ total()">
</td>
<td onClick="remove RowFromTable(th is)">-</td>
</tr>
</table>
<hr>
<table id="totalcost" border="1">
<tr>
<td>
<input type="text" size="7" name="test" readonly>
</td>
<td>
<input type="text" size="7" name="total" readonly>
</td>
</tr>
</table>
<input type="button" value="TableHTM L" onclick="showta bleinnerhtml()" >
<input type="button" value="Add" onclick="addRow ToTable();">
</form>
</body>
</html>
Jul 23 '05 #3
rick wrote:
"Lasse Reichstein Nielsen" wrote: <snip>
D.onclick = function(){remo veRowFromTable( this);};

This does not work either same reason I think? below is the
modified sample that tried. <snip> el_qty.onChange = function(){calc _total();};

<snip>

The attribute names for intrinsic events are case insensitive in HTML
but the corresponding DOM object property names are traditionally (and
mostly implemented as) all lower case. The above assignment would be
expected to create an - onChange - property and assign a reference to
the function to that property, but the function will not be called in
response to change events.

Richard.
Jul 23 '05 #4
DU
rick wrote:
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:oe******** **@hotpop.com.. .
IE doesn't translate values set with setAttribute to operative values.
To make it work in IE, you should write
el_qty.type = "text";
It should also work for modern browsers, as long as the property is
part of the W3C DOM 2 HTML specification.
I tried it but it does not work....but no error is generated.
Your suggested code does not display when I hit the 'TableHTML' button
so I think the properties is not being assigned.
My guess at what goes wrong is that ...

D.setAttribu te('onClick','r emoveRowFromTab le(this)');


.. it will definitly not recognize this one. Use this instead:
---
D.onclick = function(){remo veRowFromTable( this);};


This does not work either same reason I think? below is the
modified sample that tried.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator " content="PSPad editor, www.pspad.com">
<title>testin g tables</title>
<script type="text/javascript">
<!-- Begin Add Row to Itemlist Table
function addRowToTable() {
var tbl = document.getEle mentById('iteml ist');
var lastRow = tbl.rows.length ;
var row = tbl.insertRow(l astRow);


I recommend you use meaningful, self-explanatory variable identifiers
which are distinct from usual names found in HTML for code
maintainability +reviewing purposes.

MSIE 6 has problems with insertRow (at least in a loop) but has no
problem with
var objTRow = tbl.createEleme nt("tr");

otherwise, I would drop var lastRow = tbl.rows.length ; and use

var objTRow = objTable.insert Row(-1);

"If index is -1 or equal to the number of rows, the new row is appended."
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903

// qty cell
var qty2 = row.insertCell( 0);
You may have to use

var qty2 = document.create Element("td");

instead. Again, using insertCell with MSIE 6 should not cause problems
(except maybe when you're dynamically creating and inserting in a loop).
var el_qty = document.create Element('input' );
el_qty.type = "text";
el_qty.setAttri bute('name', 'qty');
Just like Lasse told you, best is to always avoid the setAttribute
method when there is a better, more reliable and more efficient way to
assign an attribute a value. Peter-Paul Koch recommends the same thing too.
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-89658498

el_qty.setAttri bute('size', '7');
e._qty.size = 7;
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-79659438
el_qty.setAttri bute('maxlength ', '7');
e._qty.maxLengt h = 7;
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-54719353
el_qty.onChange = function(){calc _total();};
el_qty.onchange = function(evt) {calc_total();} ;

qty2.appendChil d(el_qty);

// price cell
var price2 = row.insertCell( 1);
You may have to use

var price2 = document.create Element("td");

and then later append it to the table row object. That's how I proceeded
in a demo file because MSIE 6 was giving me problems. I did not have
problems with insertCell and insertRow with MSIE 6 as long as I did not
use these methods in a loop.

Dynamically creating, populating and inserting a table
http://www10.brinkster.com/doctorunc...tingTable.html
var el_price = document.create Element('input' );
el_price.setAtt ribute('type', 'text');
el_price.setAtt ribute('name', 'cost');
el_price.setAtt ribute('size', '7');
el_price.setAtt ribute('maxleng th', '7');
el_price.setAtt ribute('onChang e', 'calc_total()') ;
price2.appendCh ild(el_price);

// remove me cell
var D = row.insertCell( 2);
//D.innerHTML='<a href="javascrip t:removeRowFrom Table(this)">-</a>'
D.onClick = function(){remo veRowFromTable( this);};
Again, a better variable identifier can be chosen (instead of D) and
setting an event attribute value must be done differently as Lasse
explained.
var el_txt = document.create TextNode("-");
D.appendChild(e l_txt);
}
//
function getRowIndex (cell) {
return (cell.parentEle ment || cell.parentNode ).rowIndex;
Here, it would help your code efficiency a bit if you code use
offsetParent (or just parentNode) instead. Or even just use

objTableCell.pa rentNode.rowInd ex

http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-67347567

}

//Remove a row from the table
function removeRowFromTa ble(cell){
alert ("start function");
var tbl = document.getEle mentById('iteml ist');
var lastRow = tbl.rows.length ;
//alert (lastRow);
var rowToDelete = getRowIndex(cel l);
How about
var rowToDelete = cell.offsetPare nt;
and then drop the getRowIndex entirely. Also, if there are discrepancies
in the offsetParent, then there should not be for parentNode. The only
possible immediate parentNode for a table cell is a <tr> and parentNode
is supported by MSIE 5+. So,

rowToDelete = cell.parentNode ;

has to be working in MSIE 5+ and all other W3C DOM Core compliant browsers.

I'm sure there could be an even more powerful, efficient way to do this.

Something like

onclick="docume nt.getElementBy Id('itemlist'). deleteRow(this. parentNode.rowI ndex);"

(not tested but it should work).
If this single instruction works, then you replace with it some 6 lines,
2 functions, 1 function call and 3 local variables.

DU
//alert (rowToDelete);
if (lastRow > 2) tbl.deleteRow(r owToDelete);
}

// Calculate the total
function calc_total() {
//Do Nothing yet
}
function showtableinnerh tml(){
alert(document. getElementById( 'itemlist').inn erHTML);
}
//-->
</script>
</head>
<body>
<form name="myform" method="post" action="myform. php">
<table id="itemlist" border="1">
<tr><th>Qty</th><th>Cost</th><th>-</th></tr>
<tr>
<td>
<input type="text" name="qty" size="7" maxlength="7"
onChange="calc_ total()">
</td>
<td>
<input type="text" name="cost" size="7" maxlength="7"
onChange="calc_ total()">
</td>
<td onClick="remove RowFromTable(th is)">-</td>
</tr>
</table>
<hr>
<table id="totalcost" border="1">
<tr>
<td>
<input type="text" size="7" name="test" readonly>
</td>
<td>
<input type="text" size="7" name="total" readonly>
</td>
</tr>
</table>
<input type="button" value="TableHTM L" onclick="showta bleinnerhtml()" >
<input type="button" value="Add" onclick="addRow ToTable();">
</form>
</body>
</html>

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2141
by: Phil_Harvey | last post by:
I am redoing my website and trying to get it to do something more exciting using Javascript. I did normal Java at university and code at work in VB.NET. I have got reasonably far into what I want to do but am having some issues. Firstly I am using an XmlHttpRequest to pull html fragments out of files and wish to put them into the main...
7
9587
by: Coder | last post by:
Hi I have the following code in java script, it is not giving proper output in FIREFOX but running fine in IE... can anybody help me out to make this run in FIREFOX . <script language="JavaScript"> var cntlName; var eleTarget = document.getElementById('hiding'); function showOrHide(){
4
1828
by: roger31 | last post by:
Hello - i have created a simple composite control with drop down list displaying the US States. I have also a Javascript (County.js) file embedded inside the control. The Javascript file has an array of County Names. IN another Javascript i have a function that will display a list of county names for the selected state. I am using the...
11
2767
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or < or several others, it converts them to html, such as &amp; or &lt; which can sometimes cause my scripts not to work. How can I prevent ASP.NET from...
4
47963
by: auslandt | last post by:
I have an HTML file that includes a JavaScript. I would like this JavaScript to be able to create an "onChange=<function>" attribute against the password element. Here is an example page of the onChange embedded in the "password" field. This is normal HTML and it works fine. <HTML> <HEAD><TITLE> Example of onChange Event Handler </TITLE> ...
3
4123
by: lowslyn | last post by:
hi, can someone please help me with javascript... i have this code that checks a text field when the user overrides the value to blank/space. if it is blank/space, i need to change the value to zero. it messes up the some computation when the value is blank so i need to change this to zero. what is did was to add an onchange event...
6
8637
by: James007 | last post by:
Hi everyone, I am developing a web page for an embedded application. I created a text box for entering a value (only 1 byte long). The onchange event triggers correctly when I enter the value from a keyboard and press enter. However, it doesn't seem to fire when a javacript (a slider in this case) modifies the text box's value. I have...
4
5348
by: Peter | last post by:
ASP.NET I have an application which use ASP.NET Autocomplete extender which works great. But I have a question how to update all the fields on the screen using Ajax. Users starts typing in a text field which causes the Autocomplete extender to display 10 like items, after the users selects an item (which is a key in the database) I want...
19
2205
by: maya | last post by:
hi, so what is "modern" javascript?? the same as "DOM-scripting"? i.e., editing content (or changing appearance of content) dynamically by massaging javascript objects, html elements, etc? (in conjunction with css, you know, the usual...;) this is what is meant by "modern" javascript?? so how do folks feel about this who think...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.