473,387 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

onclick event in DOM table

I'm trying to dynamically build a table that allows users to remove
rows when they click a corresponding button. For some reason,
whenever I add the button to the table, it never fires the onclick
event. I'm stumped with this one, any assistance would be
appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252" />
<title>JS Table</title>

<script type="text/javascript">
<!--

var prodarray = new Array();

function plusrow() {
var mytable = document.getElementById("tabproduct");
var currentrow = mytable.insertRow();
var currentrowindex = mytable.rows.length - 1;
currentrow.id = "row" + currentrowindex;

prodarray[currentrowindex] =
document.myForm.selproduct.options[document.myForm.selproduct.selectedIndex].value;

var prodcell = currentrow.insertCell(0);
var textnode =
document.createTextNode(document.myForm.selproduct .options[document.myForm.selproduct.selectedIndex].value); //
Need to fix this to put in dropdown value
prodcell.appendChild(textnode);

var tempin1 = document.createElement("input");
tempin1.type = "text";
tempin1.id = currentrowindex + ":currentship";
tempin1.name = currentrowindex + ":currentship";
tempin1.width = "60";
var currentshipcell = currentrow.insertCell(1);
currentshipcell.appendChild(tempin1);

var tempin2 = document.createElement("input");
tempin2.type = "text";
tempin2.id = currentrowindex + ":nextship"
tempin2.name = currentrowindex + ":nextship"
tempin2.width = "60";
var nextshipcell = currentrow.insertCell(2);
nextshipcell.appendChild(tempin2);

var deletebutton = document.createElement("input"); //This button
never works in a table!
deletebutton.type = "button";
deletebutton.id = currentrowindex + ":minus";
deletebutton.name = currentrowindex + ":minus";
deletebutton.value = "-";
//deletebutton.onclick = "minusrow(" + currentrowindex + ");"; //
action I want to happen
deletebutton.onclick = "alert('event fired');"; //this doesn't even
work
var deletecell = currentrow.insertCell(3);
deletecell.appendChild(deletebutton);

//document.write(currentrow.outerHTML); //event works fine here,
comment this line to see problem.
}

function writehidden() {
var arraystring = prodarray.toString();
document.myForm.product_array.value = arraystring;
}

function minusrow(row) {
var mytable = document.getElementById("tabproduct");
mytable.deleteRow(row);
}
//-->
</script>

</head>

<body>

<form id="myForm" name="myForm" action="process.php" method="post"
onsubmit="writehidden();">
<input type="hidden" id="product_array" name="product_array" />

<select id="selproduct" name="selproduct" size="1">
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Kiwi">Kiwi</option>
<option value="Pumpkin">Pumpkin</option>
<option value="Grapes">Grapes</option>
<option value="Strawberry">Strawberry</option>
<option value="Boisenberry">Boisenberry</option>
</select>

<input type="button" id="b_addrow" name="b_addrow" value="+"
onclick="plusrow();" />

<p />
<table id="tabproduct" name="tabproduct" border="1">
</table>

<p />
<input type="submit" id="submitbutton" value="Send to Server" />

<p />
<input type="button" id="delbut" name="delbut" onclick="minusrow(1);"
value="Only way to delete row 1" />
</form>

</body>

</html>

Feb 16 '07 #1
3 6461
On Feb 16, 10:47 am, "Michael_R_Banks" <michaelrba...@satx.rr.com>
wrote:
I'm trying to dynamically build a table that allows users to remove
rows when they click a corresponding button. For some reason,
whenever I add the button to the table, it never fires the onclick
event. I'm stumped with this one, any assistance would be
appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252" />
<title>JS Table</title>

<script type="text/javascript">
<!--

var prodarray = new Array();

function plusrow() {
var mytable = document.getElementById("tabproduct");
var currentrow = mytable.insertRow();
var currentrowindex = mytable.rows.length - 1;
currentrow.id = "row" + currentrowindex;

prodarray[currentrowindex] =
document.myForm.selproduct.options[document.myForm.selproduct.selectedIndex].value;

var prodcell = currentrow.insertCell(0);
var textnode =
document.createTextNode(document.myForm.selproduct .options[document.myForm.selproduct.selectedIndex].value); //
Need to fix this to put in dropdown value
prodcell.appendChild(textnode);

var tempin1 = document.createElement("input");
tempin1.type = "text";
tempin1.id = currentrowindex + ":currentship";
tempin1.name = currentrowindex + ":currentship";
tempin1.width = "60";
var currentshipcell = currentrow.insertCell(1);
currentshipcell.appendChild(tempin1);

var tempin2 = document.createElement("input");
tempin2.type = "text";
tempin2.id = currentrowindex + ":nextship"
tempin2.name = currentrowindex + ":nextship"
tempin2.width = "60";
var nextshipcell = currentrow.insertCell(2);
nextshipcell.appendChild(tempin2);

var deletebutton = document.createElement("input"); //This button
never works in a table!
deletebutton.type = "button";
deletebutton.id = currentrowindex + ":minus";
deletebutton.name = currentrowindex + ":minus";
deletebutton.value = "-";
//deletebutton.onclick = "minusrow(" + currentrowindex + ");"; //
action I want to happen
deletebutton.onclick = "alert('event fired');"; //this doesn't even
work
var deletecell = currentrow.insertCell(3);
deletecell.appendChild(deletebutton);

//document.write(currentrow.outerHTML); //event works fine here,
comment this line to see problem.

}

function writehidden() {
var arraystring = prodarray.toString();
document.myForm.product_array.value = arraystring;

}

function minusrow(row) {
var mytable = document.getElementById("tabproduct");
mytable.deleteRow(row);

}

//-->
</script>

</head>

<body>

<form id="myForm" name="myForm" action="process.php" method="post"
onsubmit="writehidden();">
<input type="hidden" id="product_array" name="product_array" />

<select id="selproduct" name="selproduct" size="1">
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Kiwi">Kiwi</option>
<option value="Pumpkin">Pumpkin</option>
<option value="Grapes">Grapes</option>
<option value="Strawberry">Strawberry</option>
<option value="Boisenberry">Boisenberry</option>
</select>

<input type="button" id="b_addrow" name="b_addrow" value="+"
onclick="plusrow();" />

<p />
<table id="tabproduct" name="tabproduct" border="1">
</table>

<p />
<input type="submit" id="submitbutton" value="Send to Server" />

<p />
<input type="button" id="delbut" name="delbut" onclick="minusrow(1);"
value="Only way to delete row 1" />
</form>

</body>

</html>

Quick lesson in troubleshooting: When you're having a problem, reduce
it. We could have done without all of this clutter about tables,
since your problem has nothing to do with tables, and only to do with
the way you're assigning your event. So take out that one piece, the
technique you're using to assign the event, and see if it works by
itself.

It doesn't, because the onclick property of your HTMLElement object is
called as a method, and you've set it to a string value:

element.onclick = 'alert("something")'; // doesn't work

Your options include (but are not limited to):

1.
element.setAttribute('onclick', 'alert("hi")');
2.
function elClick() { alert('hi'); } // create a function object - put
this in your global scope to avoid closure memory leaks
....
// elsewhere...
element.onclick = elClick;

3.
element.onclick = new Function('alert("hi");');

And that's not including the DOM Level 2 methods, which you'll need to
learn about soon. But first, basic troubleshooting/problem reduction
skills come in mighty handy...

-David

Feb 17 '07 #2
I had a feeling it was something like that, I just am too new to JS to
get the syntax right. Thanks for the help -- the element.onclick =
new Function(xxxx); did the trick.

Regards,
Michael

Feb 17 '07 #3
Michael_R_Banks <mi***********@satx.rr.comwrote:
For some reason,
whenever I add the button to the table, it never fires the onclick
event. I'm stumped with this one, any assistance would be
appreciated.
see :
<http://www.yvon-thoraval.com/Events/onclick_event_in_DOM_table.xhtml>

i don't know if this fits your needs, but it works ))
--
Une Bévue
Feb 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andy Goldstein | last post by:
I have a table where all the TRs have an onClick handler registered. One (and only one) of the rows has 2 text input boxes, where each textbox has an onChange handler registered. Both the onClick...
8
by: cool2005 | last post by:
I tried to dynamically add/clone a <tr> from an existing <tr> to a <table> but the problem is that I need to have a "onclick" event handler in the <tr>. I have tried following A. approach...
6
by: Cockroach | last post by:
Hello, I have a problem where the onClick of a table row will activates a window.location event, and inside a cell in that row, an image onClick event shows/hides a div. The problem is that...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
10
by: Phillip Vong | last post by:
Newbie here on ASP.NET I want to hide a Table with and OnClick event. How do you do that? I'm not showing an ID when I create a table so how do I tell it to hide it? Is there a way to add an...
5
by: Jason | last post by:
Hello, I am trying to dynamically create a table, then set its <td>'s onclick: var table = document.createElement("table"); var row = table.insertRow(-1); var td = row.insertCell(-1);...
11
by: Daz | last post by:
Hello everyone. I am sure the answer to my question is simple, but I can't seem to dynamically add an onClick event to my script. I have a table which is generated dynamically, I am just...
4
by: since | last post by:
How do I in IE prevent the onclick action from being fired when I am done dragging? have tried "window.event.cancelBubble = true", for onmouseup , onmousedown, and onmousemove handlers. The onclick...
11
by: nma | last post by:
Hi How can I make the anchor link once I do onclick? <td width="<?php echo ($cellwidth); ?>" height="17" bgcolor="#00ff00" onMouseOver="this.style.background ='red'" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.