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)";

See my response here:

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]);
        });
    }
});
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.