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

P

Peter

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>
 
T

Thomas 'PointedEars' Lahn

Peter said:
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
 
R

RobG

Peter said:
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:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top