0

Hello everyone,
I need help with my javascript code. I'm trying to create a dynamic table with onclick event like this but onclick event seems to do nothing:

var srcTable = this.iContext.getElementById("tbody");
 		var tmpRow = null;
 		var tmpCell = null;
 		var i;

 		
 		for(i=0; i<5; i++)
 		{
 			tmpRow = srcTable.insertRow();
 			tmpRow.onclick = function() { doSomething(this);};
 			
 			tmpCell = tmpRow.insertCell(tmpRow);
 			tmpCell.innerText = "Author";
 			tmpCell = null;
 			
 			tmpCell = tmpRow.insertCell(tmpRow);
 			tmpCell.innerText = i;
 			tmpCell = null;
 			
 			tmpRow = null;
 		}

I found a few examples like this:

tmpRow.onclick = function() { alert(this.rowIndex);};

but I need use selected data, maybe call a defined function. I already tried lines bellow but nothing works, because doSomething is not function of tmpRow object.
Can somebody help?

tmpRow.onclick = function() { doSomething(this);};

or

tmpRow.onclick = doSomething(this);

or

tmpRow.onclick = "doSomething(this)";
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by R0bb0b
0

I've never done bound a click event handler to a table row but you could try:

include this in the head section:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

here would be your javascript

var srcTable = this.iContext.getElementById("tbody");
 		var tmpRow = null;
 		var tmpCell = null;
 		var i;

                var arrRowOnclicks = [];
 		
 		for(i=0; i<5; i++)
 		{
 			tmpRow = srcTable.insertRow();
                        tmpRow.id = "row"+i;  //give the row an id so that it can be bound with jquery event
 			
 			tmpCell = tmpRow.insertCell(tmpRow);
 			tmpCell.innerText = "Author";
 			tmpCell = null;
 			
 			tmpCell = tmpRow.insertCell(tmpRow);
 			tmpCell.innerText = i;

                        arrRowOnclicks[i] = "row"+i;  //collection of row ids to bind a click event handler to

 			tmpCell = null;
 			
 			tmpRow = null;
 		}

function runRowOnclick(rowID)
{
    //do something
}

$(document).ready(function()
{
    for(i = 0; i < arrRowOnclicks.length; i++)
    {
        $("#"+arrRowOnclicks[i]).click(function()
        {
            runRowOnclick(arrRowOnclicks[i]);
        });
    }
});

Edited by R0bb0b: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.