It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

IE innerHTML in a table dont work!

IE innerHTML in a table dont work!

Postby mkariti on Tue May 24, 2011 6:26 pm

Hi there,
Ive check the following code in all major browser and it works fine accept ofcourse in IE.
Im trying to take the innerHTML from one row and move it to the one above or below depand on the button you are clicking.
Does anyone have any idea?

Many thanks
mkariti

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
TABLE {
border-collapse: collapse;
}
TBODY {
border-top: solid black 1px;
}
TR {
border-bottom: solid black 1px;
}
</style>
<script type="text/javascript" language="javascript">

var getId;
var row = {
selected: -1,
hoverColor: '#CCCCCC',
defaultColor: '#FFFFFF',
onclick: function (trow, id) {
getId = id;

var r = document.getElementById('row_' + row.selected);
if (row.selected != -1 && r)
r.bgColor = row.defaultColor;
row.selected = id;
trow.bgColor = row.hoverColor;

},
moshe: function (num) {

var table = document.getElementById("tableId");
var getRowLength = document.getElementById("tableId").rows.length;

if (num == 1) {

// table.rows[getId].parentNode.insertBefore(table.rows[getId], table.rows[getId - 1]);
// getId++;
var tempDown = document.getElementById("row_" + getId).innerHTML;
document.getElementById("row_" + getId).innerHTML = document.getElementById("row_" + (getId + 1)).innerHTML;
document.getElementById("row_" + getId).bgColor = row.defaultColor;
document.getElementById("row_" + (getId + 1)).innerHTML = tempDown;
document.getElementById("row_" + (getId + 1)).bgColor = row.hoverColor;
getId++;


}
else {
// getId--;
// table.rows[getId].parentNode.insertBefore(table.rows[getId + 1], table.rows[getId]);

var tempUp = document.getElementById("row_" + getId).innerHTML;
document.getElementById("row_" + getId).innerHTML = document.getElementById("row_" + (getId - 1)).innerHTML;
document.getElementById("row_" + getId).bgColor = row.defaultColor;
document.getElementById("row_" + (getId - 1)).innerHTML = tempUp;
document.getElementById("row_" + (getId - 1)).bgColor = row.hoverColor;

getId--;

}

}

}

</script>
</head>
<body>
<div>
<table id="tableId">
<tbody>
<tr id="row_1" onclick="row.onclick(this,1);"><td><input type="checkbox"></td><td>Row 1</td><td>A</td></tr>
<tr id="row_2" onclick="row.onclick(this,2);"><td><input type="checkbox"></td><td>Row 2</td><td>B</td></tr>
<tr id="row_3" onclick="row.onclick(this,3);"><td><input type="checkbox"></td><td>Row 3</td><td>C</td></tr>
<tr id="row_4" onclick="row.onclick(this,4);"><td><input type="checkbox"></td><td>Row 4</td><td>D</td></tr>
<tr id="row_5" onclick="row.onclick(this,5);"><td><input type="checkbox"></td><td>Row 5</td><td>E</td></tr>
</tbody>
</table>
</div>
<div>
<button id="btn_down" onclick="row.moshe(1)">Down</button>
<button id="btn_up" onclick="row.moshe(2)">Up</button>

</div>
</body>
</html>
mkariti
 
Posts: 1
Joined: Tue May 24, 2011 6:22 pm

Re: IE innerHTML in a table dont work!

Postby rajmv on Sun Jul 17, 2011 7:19 am

You can use .insertBefore(), but you have to use it as follows;

Code: Select all
var getId;
var row = {
selected: -1,
hoverColor: '#CCCCCC',
defaultColor: '#FFFFFF',
   onclick: function (trow) {
      
      if (trow.tagName.toUpperCase()=='INPUT') trow = trow.parentNode.parentNode;
      var id = parseInt(trow.id.replace(/row_/,''));
      getId = id;
      var r = document.getElementById('row_' + row.selected);
      if (row.selected != -1 && r) r.style.backgroundColor = row.defaultColor;
      row.selected = id;
      trow.style.backgroundColor = row.hoverColor;
   },
   moshe: function (num) {
      var table = document.getElementById("tableId");
      var getRowLength = document.getElementById("tableId").rows.length;
      if (num == 1) {
         document.getElementById("row_" + (getId+1)).style.backgroundColor= row.defaultColor;
         document.getElementById("row_" + (getId)).style.backgroundColor= row.hoverColor;
         document.getElementById('tablebody').insertBefore (document.getElementById('tablebody').removeChild(document.getElementById('row_'+(getId+1))), document.getElementById('row_'+(getId)));
         
         row.onclick (document.getElementById('row_'+(getId)));
         getId++;
      
      } else {
         document.getElementById("row_" + (getId-1)).style.backgroundColor= row.defaultColor;
         document.getElementById("row_" + (getId)).style.backgroundColor= row.hoverColor;
         document.getElementById('tablebody').insertBefore (document.getElementById('tablebody').removeChild(document.getElementById('row_'+(getId))), document.getElementById('row_'+(getId-1)));
         row.onclick (document.getElementById('row_'+(getId)));
         getId--;
         
      }
      
   }

}


Your code is still messy though, as pressing "down" or "up" twice for an item will produce bad results. The problem is caused by the fact that you use numbered ID's while you also want to change the ordering of those numbers.
rajmv
100+ Club
 
Posts: 103
Joined: Thu Jul 14, 2011 7:22 am


Who is online

Users browsing this forum: No registered users and 2 guests