Jump to content


Photo

Simple dynamic variable naming

dynamic variables

Best Answer spertuit, 05 September 2013 - 02:44 PM

Wow, must be too early this morning 

            var cell2 = row.insertCell(1);
            var element2 = document.createElement("input");
            element2.type = "checkbox";
            element2.name="breakfast" + rowCount + "[]";
			element2.value = "breakfast";
            cell2.appendChild(element2);

rowCount does the trick

Go to the full post


  • Please log in to reply
1 reply to this topic

#1 spertuit

spertuit
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts
  • LocationLouisiana

Posted 05 September 2013 - 02:35 PM

This should be pretty easy but I can get this to work right. I am dynamically adding rows to a table and I want to append the number of the current row to the variable, these variables are arrays. (ex. variable1[], variable2[])

 

Example Code

        function addRow(tableID) {
 
            var table = document.getElementById(tableID);
 
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
	    var currentRow = $(this).closest('tr');
 
	    var cell1 = row.insertCell(0);
	    cell1.style.textAlign = 'left';
            cell1.innerHTML = "<a onClick=\"deleteRow('dataTable', this.parentNode.parentNode.rowIndex)\" align=\"left\"> Remove this Guest</a>";
 
            var cell2 = row.insertCell(1);
            var element2 = document.createElement("input");
            element2.type = "checkbox";
            element2.name="breakfast" + currentRow + "[]";
            cell2.appendChild(element2);
 
            var cell3 = row.insertCell(2);
            var element2 = document.createElement("input");
            element2.type = "checkbox";
            element2.name="breakfast" + row + "[]";
            cell3.appendChild(element2);

So, I've tried using currentRow and row, still not working. Anyone have ideas?

 


Database, no problem. Just Google "Truncate."

#2 spertuit

spertuit
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts
  • LocationLouisiana

Posted 05 September 2013 - 02:44 PM   Best Answer

Wow, must be too early this morning 

            var cell2 = row.insertCell(1);
            var element2 = document.createElement("input");
            element2.type = "checkbox";
            element2.name="breakfast" + rowCount + "[]";
			element2.value = "breakfast";
            cell2.appendChild(element2);

rowCount does the trick


Database, no problem. Just Google "Truncate."




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users