473,387 Members | 1,590 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.

cell.onclick="..." not work?

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 inserttable()
{
alert("running");
var oRow = testtable.insertRow();
var cell = oRow.insertCell();
cell.innerHTML = "hello";
cell.onclick="javascript:alert('hello');";
cell.onmouseover = "javascript:alert('mouseOver');"
return;
}
</script>
</head>
<body>
<table>
<tr>
<td><input type="button" name="Test" value="Click"
onclick="javascript:inserttable();"></td>
</tr>
<tbody id="testtable">
</tbody>
</table>
</body>
</html>

Feb 13 '06 #1
2 8028
Peter wrote:
cell.onclick="javascript:alert('hello');";


Read about such `javascript:' nonsense in this newsgroup's FAQ.
Furthermore, the value of the (proprietary) `onclick' property
of DOM element objects has to be a Function object reference,
`null' or `undefined'. Since you want the UA do something on
event:

cell.onclick = function()
{
alert('hello');
};

BTW, this is definitely a FAQ here. Please do research on previous
articles before you post, it is both very annoying and tiresome to
answer the same questions all over again. It is called _news_group
for a reason.

BTW 2: Your HTML code is not Valid markup.

<URL:http://validator.w3.org/>
PointedEars
Feb 13 '06 #2
Peter wrote:
Hi, this is the code, and new row and new cell generated ok, but why
the
onclick and onmouseover doen't work?
Because there are errors in the script.

Thank you in advance!

<html>
<head>
<script language="javascript">
The language attribute is deprecated, type is required:

<script type="text/javascript">

function inserttable()
{
alert("running");
var oRow = testtable.insertRow();
var cell = oRow.insertCell();
cell.innerHTML = "hello";
cell.onclick="javascript:alert('hello');";
'javascript:' is typically used as a pseudo-protocol in the value of a
href attribute of an A element, e.g.:

<a href="javascript:someFn();" ... >

However usually a real href should be supplied and an onclick handler
used for the script:

<a href="realPage" onclick="someFn();return false;" ... >
When setting the value of an onclick property using script, you can't
just plug in arbitrary code, use either an anonymous function:

cell.onclick = function(){alert('hello');}
or define the function elsewhere and pass a reference:

function sayHello()
{
alert('hello');
}
function inserttable()
{
...
cell.onclick = sayHello; // Note lack of '()'
...
}

cell.onmouseover = "javascript:alert('mouseOver');"

Same for onmouseover.

return;
}
</script>
</head>
<body>
<table>
<tr>
<td><input type="button" name="Test" value="Click"
onclick="javascript:inserttable();"></td>
Here 'javascript:' should be omitted, leaving just the function call:

<td ... onclick="inserttable();"></td>

And it is normal to give a button a more meaningful label than 'click',
but I guess this is just an example.

</tr>
<tbody id="testtable">
A tbody element without rows is invalid HTML ... but not too many user
agents will quibble over that.

</tbody>
</table>
</body>
</html>


--
Rob
Feb 13 '06 #3

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

Similar topics

10
by: Eric-Sebastien Lachance | last post by:
Hey there, I decided to just create a 100% height and width div that filled the space over a background header image, and add an onclick event to redirect to the my index... Doesn't seem to work...
5
by: johnsuth | last post by:
I want to produce a trivial demonstration of dynamic modification. I thought that pressing a button might change its color. I studied O'Reillys books and successfully created the button with a...
13
by: alvin.yk | last post by:
Hi, Normally, a piece of code such as <a href="http://www.yahoo.com" onclick="alert('hello');return false;">link</a> will stop the browser from actually going to href's destination....
11
by: jesdynf | last post by:
I'm having trouble applying a stylesheet to content I'm generating after the fact. Here's the sample code: <html> <head> <title>CSS/DOM Problem Example</title> <style type="text/css">...
1
by: grantmx | last post by:
Hey guys - We am having problems with the following code in our form: <tr><td></td><td align=right><input type="image" name="_ctl25:btnSaveLarge" onclick="if (typeof(Page_ClientValidate) ==...
4
by: John Nagle | last post by:
I'm printing out each entry in "gc.garbage" after a garbage collection in DEBUG_LEAK mode, and I'm seeing many entries like <cell at 0x00F7C170: function object at 0x00FDD6B0> That's the...
2
by: ejrfigueiredo | last post by:
Hi, Is there a way to get a script function to save a page without the execCommand (which works on IE but doesn't on Firefox). Below there's the code I'm using, I took it from a template page...
5
by: dangt85 | last post by:
Hello, I have the following page: ... <style type="text/css"> body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.