views:

26

answers:

3

Hello I have a table which has a few rows. And I have an add button in my application which allows to add rows at any rowIndex. I use insertRow method to insert rows at a specific position. So once a row in inserted, I change the table row Ids of all the rows, to arrange them in an ascending order. Now I have been developing this application in FF and it has been coming pretty well. Now I am making a few changes to the code to make it work in IE. But this just does not work in IE. I have been testing this code for the last two days, it works in FF and chrome, but not in IE. I am not sure, what is the mistake I am making. I am just recreating the situation with an example, and this is the code for that example. Please help me out and tell me what could be the mistake I am making for it not to be working in IE. Any suggestions would be a great help.

<html>
<head>
<script type = 'text/javascript'>
function getIds()
{
var elem = document.getElementsByTagName("tr");

 for(var i in elem)
 {
 if(elem[i] && elem[i].id!=null && elem[i].id!='')
 alert(elem[i].id);
 }
}

function changeIds()
{
var elem = document.getElementsByTagName("tr");

 for(var i in elem)
 {
 if(elem[i] && elem[i].id!=null && elem[i].id!='')
 { index = Number(elem[i].rowIndex)+1; elem[i].id = "tabsf_"+index;} 

 }
 alert('change');
}
</script>
</head>
<body>
<table id="tabsf">
 <tbody>
  <tr id="tabsf_1"><td>1</td><td>2</td></tr>
  <tr id="tabsf_2"><td>3</td><td>4</td></tr>
  <tr id="tabsf_5"><td>5</td><td>6</td></tr>
  <tr id="tabsf_3"><td>7</td><td>8</td></tr>
  <tr id="tabsf_4"><td>9</td><td>10</td></tr>
 </tbody>
</table>
<table><tr><td><input type="button" name="hach" value="getIds" onclick="getIds()" /></td>
<td><input type="button" name="hach" value="Change Ids" onclick="changeIds()" /></td></tr></table>
</body>
</html>
</code>
A: 

Update: Try @Spinon's idea first.

Update: It seems to in fact have been an ID collision, something IE rightly prevented from happening and the other browsers happily let through.

Original answer:

My guess is that your loop creates an id collision: An id is set twice while the loop runs. IE could (rightly) be blocking the setting of a duplicate ID.

I would try unsetting the IDs first:

for(var i in elem)
 {

 if(elem[i] && elem[i].id!=null && elem[i].id!='')
  elem[i].id =  elem[i].id + "_temp";    
 }


for(var i in elem)
 {
 if(elem[i] && elem[i].id!=null && elem[i].id!='')
 { index = Number(elem[i].rowIndex)+1; 
   elem[i].getElementsByTagName("td")[0].innerHTML = "tabsf_"+index;    
   elem[i].id = "tabsf_"+index;} 

 }

However, I don't understand how you use row IDs to change the sorting?

Pekka
Well these rows have child elements which have the required data. On a PHP submit, I get all elements in each row, get the data in them and post them to my database. The data is arranged based on these row IDs.
sai
But when I do that it says 'undefined' is null or not an object. So I tried to setAttribute for that element using elem[i].id = null;elem[i].setAttribute("id", "tabsf_"+index);But it still gives me the same error.
sai
@sai my bad, sorry. I changed the mechanism a bit and added a check.
Pekka
please don't mind me asking, did that work for you in IE?
sai
it is giving me the same undefined is null or not an object error :(
sai
@sai I haven't tested it, I'll do that now.
Pekka
That will be a great help Sir. Thanks!
sai
@sai the version I posted works for me. Can you try again? I added a line that shows the sorting effect immediately, just remove that for real world use.
Pekka
thanks a lot Sir!! you are the man!! but I still wonder why was it working in other browsers :| and why is IE so complex??
sai
@sai if this was the problem, IE is the one that got it right: The other browsers are overly tolerant. A duplicate ID leads to that element not being able to be loaded using `getElementById` anymore, something that the other browsers should prevent.
Pekka
Ok! I guess this surely is my lesson to learn then!! thanks a lot!!
sai
A: 

I would say try using the setAttribute function vs the id property to change the id.

spinon
Hello Spion I used setAttribute function elem[i].setAttribute("id", "tabsf_"+index); The wierd thing that happened was row Ids were changed in this way 1,2,3,3,5. What could this mean?? It should have been 1,2,3,4,5. I am using IE developer tools to view the dom and that shows the rowIds
sai
A: 

The problem is that for..in loop doesn't ensure order. It is one of the gotchas why you should avoid them for arrays. (there is another, see my link) Use simple for loops instead ( for i..n )

function getIds() {
  var elem = document.getElementsByTagName("tr");
  for (var i = 0; i < elem.length; i++) {
    if (elem[i].id) {
      alert(elem[i].id);
    }
  }
}

function changeIds() {
  var elem = document.getElementsByTagName("tr");
  for (var i = 0; i < elem.length; i++) {
    if (elem[i].id) {
      index = Number(elem[i].rowIndex) + 1;
      elem[i].id = "tabsf_" + index;
    }
  }
  alert('change');
}

Works perfectly cross-browser.

galambalazs