Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with changing content of a table 1

Status
Not open for further replies.

alex1020

MIS
Feb 6, 2008
3
DE
Hello ,
I have problem with changing content of a Dynamic table in FireFox ! ( mean its working fine in IE )
since i created the table dynamically i am using following function to change contents of the table .

*********************
function test1(text,row,col){
var x=document.getElementById('tableid').rows[row].cells;
x[col].innerHTML=text;
return;
}
***********************

its working fine in IE but its showing following error in firefox ( in debung section )

******************
Error: x[col] has no properties.
*******************

but if i change the input of rows to a static one its working fine ! but its updating just content of a row !
 
Well... there's not really much we can say based on the code you provided because there's nothing wrong with it. You're getting the error because you're trying to access a table element that doesn't exist. The only thing I can suggest is to alert the values of row and col when the code crashes. It's likely that they don't contain the values you think they do. There isn't anything magical about how the rows and cells collections work in FF - it's the same as IE. Run this test in IE and FF and you'll see they produce the same output, and error on the same line (although IE will give you the super descriptive error text [Object error] because IE's error handling sucks balls):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

window.onload = function () {
   var t = document.getElementById("t");
   alert(t.rows[1].cells[1].innerHTML);
   t.rows[1].cells[1].innerHTML = 5;
   try {
      t.rows[1].cells[2].innterHTML = "this will break";
   }
   catch (e) {
      alert("cell does not exist\n\n" + e);
   }
};

</script>
<style type="text/css"></style>
</head>
<body>

<table id="t">
   <tr>
      <td>1</td>
      <td>2</td>
   </tr>
   <tr>
      <td>3</td>
      <td>4</td>
   </tr>
</table>

</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Are you sure that [tt]col[/tt] is being passed as an integer?
Try:
Code:
x[parseInt(col,10)].innerHTML=text;
And see how that goes.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
>>dwarfthrower
i did but no difference

///
thanks kaht , i did a test , maybe this help you to give me a solution

i tested it ( without function !)

var x=document.getElementById('tableid').rows[1].cells;
x[1].innerHTML="test";

above code works fine ! but when i repeated it ,it showed same error :

var x=document.getElementById('tableid').rows[1].cells;
x[1].innerHTML="test";

var x2=document.getElementById('tableid').rows[2].cells;
x2[2].innerHTML="test";


i found out its working only on one row ! when i am using a new row its showing error :\

mean following code is working :

var x=document.getElementById('tableid').rows[1].cells;
x[1].innerHTML="test";

var x2=document.getElementById('tableid').rows[1].cells;
x2[2].innerHTML="test";


but this one NO !

var x=document.getElementById('tableid').rows[1].cells;
x[1].innerHTML="test";

var x2=document.getElementById('tableid').rows[2].cells;
x2[2].innerHTML="test";
 
It might help you to find out what the length of your collections are as well. Try putting this in on the lines that error:
Code:
alert("# of rows: " + document.getElementById('tableid').rows.length);
var x=document.getElementById('tableid').rows[1].cells;
alert("# of cells in row 2: " + x.length);


One suspicious thing that pops up from your code above is that you're using 1s and 2s when explicitly referencing the rows and cells. Javascript arrays are 0 based, meaning that the first element of an array is the 0th element.

In your code above are you trying to reference cells 1 and 2? If so then you should be referencing the 0 and 1 elements of the collection.


-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
thanks kaht for your attention

///

i tried your code and this was the output :
# of rows:28
# of cells in row 2:0
//

the problem is not with cell 0 or 1 ( here it was only a test )
if i use a static row its working fine ! but once i am trying to use more than one row its not working :\

//

becouse my table is dynamic i cant change their contents using TD ID :\

do you know any solution for this problem ?
i want to update content of a dynamic table ( cells and rows )

thanks !
 
i want to update content of a dynamic table

That's the problem, you're not updating the content because the content doesn't exist to be updated.

You can see that in your last reply:

# of rows:28
# of cells in row 2:[!]0[/!]

You can't update something if it isn't there. You have to create the cells and insert them into the row before you can update their values. Look into the [google]javascript createElement[/google] dom method.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top