473,394 Members | 1,869 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,394 software developers and data experts.

onClick from Table Cell Help

Hello,
I'm trying to figure out why the following code won't work for me. The
Firefox javascript console tells me that tableItemClicked is not
defined, but the function is right there in the code. Any help would be
greatly appreciated.

<script type="text/javascript">

function Initialize() //is called upon page load
{
MakeTable();
}//end Initialize

function MakeTable(){
var iNumCells=10;
var text="";
var counter = 1;

document.write("<table><table border = 1><tr>");// "table width='50%'
name='mytable'")
for (counter = 1; counter <= iNumCells; counter++){
text = "";
//funcText="TableItemClicked('" + counter + "');";

//create a new row every 5 items
if(counter % 5 == 0 && counter!=0){
text = text + "<td id=" + counter + " "+ "
onClick=\"tableItemClicked();\">" + "Item: " + counter + "</td></tr>";
alert(text);
document.write(text);
}

else if (counter - 1 % 5 == 0){
text = "<tr><td>Item: " + counter + "</td>";
document.write(text);
}

else{
text = "<td>Item: " + counter + "</td>";
document.write(text);
}
}
document.write("</table>");

document.write("<p><div id = 'targetDiv'>Item number will appear
here</div></p>");
}// end MakeTable

function tableItemClicked()
{
alert("tableItemClicked");
//document.getElementById('targetDiv').innerHTML = "You clicked item
number " + counter;
}//end TableItemClick

</script>
Apr 20 '06 #1
7 2098
news.east.cox.net wrote:
Hello,
I'm trying to figure out why the following code won't work for me.
The Firefox javascript console tells me that tableItemClicked is not
defined, but the function is right there in the code. Any help would be
greatly appreciated.

Use DOM methods to build your table, it's much easier to read.

--
Ian Collins.
Apr 20 '06 #2
I will try the DOM method, but I don't know if that will solve my
problem. I think there is something I'm not understanding about calling
a function from another function. I'm pretty sure that my
misunderstanding would follow me to DOM.

The actual code to create the tag I'm having trouble with is:
text = text + "<td id=" + counter + " "+ "
onClick=\"tableItemClicked();\">" + "Item: " + counter + "</td></tr>";

The tag itself reads:
<td id=5 onClick="tableItemClicked();">Item 5</td>

JavaScript Console in Firefox reports that:
tableItemClicked is not defined

Ian Collins wrote:
news.east.cox.net wrote:
Hello,
I'm trying to figure out why the following code won't work for me.
The Firefox javascript console tells me that tableItemClicked is not
defined, but the function is right there in the code. Any help would be
greatly appreciated.

Use DOM methods to build your table, it's much easier to read.

Apr 21 '06 #3

news.east.cox.net wrote:

function Initialize() //is called upon page load
{
MakeTable();
}//end Initialize


If that comment is accurate, it means that you're writing to a closed
document, which will have rather shaky results. How exactly are you
calling Initialize()?

I notice that your code appears to open two tables but closes only one.

The '/' character in closing tags ideally should be escaped: <\/td>
--
S.C.

Apr 21 '06 #4
Stephen Chalmers said on 21/04/2006 11:15 AM AEST:
news.east.cox.net wrote:

function Initialize() //is called upon page load
{
MakeTable();
}//end Initialize

If that comment is accurate, it means that you're writing to a closed
document, which will have rather shaky results.


Not really shaky, a call to document.write() after the document has
loaded will call document.open(), which will remove the entire content
of the page.

The OP's code results in the following HTML:

<TABLE>
<TBODY></TBODY>
</TABLE>
<TABLE border=1>
<TBODY>
<TR>
<TR>
<TD>Item: 1</TD>
<TD>Item: 2</TD>
<TD>Item: 3</TD>
<TD>Item: 4</TD>
<TD id=5 onclick=tableItemClicked();>Item: 5</TD>
</TR>
<TR>
<TD>Item: 6</TD>
<TD>Item: 7</TD>
<TD>Item: 8</TD>
<TD>Item: 9</TD>
<TD id="10" onclick="tableItemClicked();">Item: 10</TD>
</TR>
</TBODY>
</TABLE>
<P>
<DIV id="targetDiv">Item number will appear here</DIV>
<P></P>
The reason calling 'tableItemClicked()' returns undefined is because the
function is not defined anywhere - it was deleted by document.open()
when document.write() was called.

If the Initialize() function is called during page load, the onclick
'works'.

[...]
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 21 '06 #5
Stephen Chalmers wrote:
news.east.cox.net wrote:

function Initialize() //is called upon page load
{
MakeTable();
}//end Initialize

If that comment is accurate, it means that you're writing to a closed
document, which will have rather shaky results. How exactly are you
calling Initialize()?

I notice that your code appears to open two tables but closes only one.

Another good reason to use DOM!

--
Ian Collins.
Apr 21 '06 #6
This is how I am calling Initialize()

<body onload="Initialize()" >
</body>

So, I am calling it during page load

RobG wrote:
Stephen Chalmers said on 21/04/2006 11:15 AM AEST:
news.east.cox.net wrote:

function Initialize() //is called upon page load
{
MakeTable();
}//end Initialize

If that comment is accurate, it means that you're writing to a closed
document, which will have rather shaky results.


Not really shaky, a call to document.write() after the document has
loaded will call document.open(), which will remove the entire content
of the page.

The OP's code results in the following HTML:

<TABLE>
<TBODY></TBODY>
</TABLE>
<TABLE border=1>
<TBODY>
<TR>
<TR>
<TD>Item: 1</TD>
<TD>Item: 2</TD>
<TD>Item: 3</TD>
<TD>Item: 4</TD>
<TD id=5 onclick=tableItemClicked();>Item: 5</TD>
</TR>
<TR>
<TD>Item: 6</TD>
<TD>Item: 7</TD>
<TD>Item: 8</TD>
<TD>Item: 9</TD>
<TD id="10" onclick="tableItemClicked();">Item: 10</TD>
</TR>
</TBODY>
</TABLE>
<P>
<DIV id="targetDiv">Item number will appear here</DIV>
<P></P>
The reason calling 'tableItemClicked()' returns undefined is because the
function is not defined anywhere - it was deleted by document.open()
when document.write() was called.

If the Initialize() function is called during page load, the onclick
'works'.

[...]


Apr 21 '06 #7
Ian Collins wrote:
Stephen Chalmers wrote:
news.east.cox.net wrote:

function Initialize() //is called upon page load
{
MakeTable();
}//end Initialize


If that comment is accurate, it means that you're writing to a closed
document, which will have rather shaky results. How exactly are you
calling Initialize()?

I notice that your code appears to open two tables but closes only one.

Another good reason to use DOM!


DOM solved the problem. I'm still not sure what I did wrong in the
first instance, but using DOM solved it. Thanks for the help.
Apr 22 '06 #8

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

Similar topics

2
by: Barton | last post by:
Hello, I've got a table row with an onClick event that loads page1. Now I do not want to apply this event to one cell of that row because that cell contains already a link to another page. How...
2
by: Andreas Knollmann | last post by:
Hi, I create an object like this: var cell = document.createElement("td"). It doesn't have to be cell. I want this cell to use the onclick event. What doesn't work in the IE as well as with...
1
by: Michael | last post by:
How can I use onclick to change a table cell background color. I know I can use this.className=? to change the classname. But then I would have to put the code into every single table cell. I...
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...
2
by: Peter | last post by:
Hi, this is the code, and new row and new cell generated ok, but why the onclick and onmouseover doen't work? Thank you in advance! <html> <head> <script language="javascript"> function...
4
by: jj6849 | last post by:
I have been using the dom to add a row to my form for awhile now, but now I need to do some validation to make sure certain check boxes aren't checked with other check boxes. Now of course it works...
18
by: joaotsetsemoita | last post by:
Hello everyone, I'm having troubles assigning an onclick event to a cell. Im trying something like cursorPoint.cells.style.cursor = "hand"; cursorPoint.cells.width = "20";...
4
by: prosad | last post by:
hello, Just solved a problem using Javascript onclick, can click on any cell in a dynamic table and it will pass the innerText object value to my form text field. parts of code given below: ...
7
by: KDawg44 | last post by:
Hi, I have a series of notes in the cells of a table, when they click the note, it calls a function that creates a text area for the user to edit the note in. What I would like to do is have it...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.