Dear forumchanin! I do javascript permutation of table columns, but this code does not want to work. As an important condition here, there is no way to edit the html structure in any way, including appending to the table cell or the table itself no id and name. All are denoted as class = t1. The task using the function samopisnoy rearrange the values ​​of the columns. Tell me the right direction or point out an error, because javascript only knows the basics and recently. I would be grateful for a constructive response.

  • Well, I indicated a link to the sample code, I think it’s clear from the code, there is a button and a table. The inversion (2.1) function replaces the 2 by 1 column, and the 1st by the 2nd, taking into account the fact that it is counting from zero. It is called on the onclick event on the button. - IntegralAL
  • in ie see? swapNode is only there. - Yura Ivanov
  • In general, they said that in all browsers the code was plowed, in ie, too. I thought it would work everywhere ... - IntegralAL

2 answers 2

You can probably fix it this way:

function inversion(first_column, second_column){ var mytable1 = document.getElementsByClassName("t1")[0]; if (mytable1 && mytable1.tagName != "TABLE") { alert("Таблица не найдена!"); return; } var tempvar = ''; alert("Цикл начался!"); for (var i=0; i<mytable1.rows.length; i++) { var temp = mytable1.rows[i].cells[first_column].innerHTML; mytable1.rows[i].cells[first_column].innerHTML = mytable1.rows[i].cells[second_column].innerHTML; mytable1.rows[i].cells[second_column].innerHTML = temp; } alert("Цикл кончился!"); } 
  • Thank! Earned). I, too, at first tended to the temp variable to exchange the cell data, but then for some reason I thought that swapNode would be better. But it turned out that no ... - IntegralAL

it is still possible (on large tables it will be significantly faster):

 function inversion(first_column, second_column) { var mytable1 = document.querySelector("table.t1"); for (var i = 0, row; row = mytable1.rows[i], i < mytable1.rows.length; i++) { var c1 = row.cells[first_column], c2 = row.cells[second_column], c11 = c1.cloneNode(); row.replaceChild(c11, c2); row.replaceChild(c2, c1); }; }