Thanks Nushi - that isn't the problem. (BTW lowercase tr works fine too). The tr element is being created correctly.
What I need is to add an event to this element.
Main Topics
Browse All TopicsIn my code, I have a
trow = document.createElement ("tr");
trow = orderdetails.insertRow();
Is there a way I could create an event (say Click) to this element? Something like
onclick = "alert('row clicked');" ??
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script>
function addRow(){
var table = document.getElementById('t
var TR = table1.insertRow();
var TD = TR.insertCell().innerHTML = 'Row added';
}
</script>
<table id='table1'>
</table>
<br>
<input type='button' value='Add Row' onclick='addRow()'>
</body>
</html>
Nushi.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script>
function addRow(){
var table = document.getElementById('t
var TR = table1.insertRow();
var TD = TR.insertCell();
TD.innerHTML = 'Row added';
TD.onclick= function(){ alert( 'This is onclick') };
TD.onmouseover= function(){ alert( 'This is mouseover') };
}
</script>
<table id='table1'>
</table>
<br>
<input type='button' value='Add Row' onclick='addRow()'>
</body>
</html>
Nushi - Another question. When are the parameters resolved? For example in the code below:
for(i = 1; i <= objLst.length -1; i++)
{
if (j == 1)
{
trow = document.createElement ("tr");
trow = orderdetails.insertRow();
curroid = objLst.item(i).text;
alert(curroid) ; //-----> this shows the correct id
trow.onclick= function(){ showProducts(curroid); };
}
.....
.....
But when I have an alert within showProducts, the input argument is always the last of curroids.
For example if I have three rows with ids 1, 2, 3 - no matter from where I call showProducts, the input is always 3.
Thanks!
Hello svid,
put the var declaration for curroid Outside of the function definition and assign the latest value in the function.
The onclick event is outside of the scope where the curroid is defined if curroid is not global.
Like this:
var curroid;
function xyz(){
//...
for(i = 1; i <= objLst.length -1; i++){
if (j == 1){
trow = document.createElement ("tr");
trow = orderdetails.insertRow();
curroid = objLst.item(i).text;
alert(curroid) ; //-----> this shows the correct id
trow.onclick= function(){ showProducts(curroid); };
}
}
Good luck,
ZeroPage
Sorry, wrong.
var curroid;
function xyz(){
//...
for(i = 1; i <= objLst.length -1; i++){
if (j == 1){
trow = document.createElement ("tr");
trow = orderdetails.insertRow();
curroid = objLst.item(i).text;
alert(curroid) ; //-----> this shows the correct id
trow.onclick= function(){ showProducts(); };
}
}
function showProducts(){
alert(curroid);
}
By making it global, wont it always be the latest value?
for example, I have 3 rows
1 - on click of this row I want showProducts(1)
2 - on click of this row I want showProducts(2)
3 - on click of this row I want showProducts(3)
By making it global, after row 3 is created, curroid would be 3
so ShowProducts will always execute with 3, no matter which row is selected.
What I would like is the foll, but with everything created at runtime
<table>
<tr onclick= "showProducts(1)">....</tr
<tr onclick= "showProducts(2)">....</tr
<tr onclick= "showProducts(3)">....</tr
</table>
Thanks for your help.
Ok, then change it to this:
function xyz(){
//...
for(i = 1; i <= objLst.length -1; i++){
if (j == 1){
trow = document.createElement ("tr");
trow = orderdetails.insertRow();
curroid = objLst.item(i).text;
alert(curroid) ; //-----> this shows the correct id
trow.onclick= new Function("showProducts("+c
}
}
Business Accounts
Answer for Membership
by: NushiPosted on 2004-01-07 at 11:07:58ID: 10064535
trow = document.createElement ("TR"); //must be CAPITAL
trow = orderdetails.insertRow();