468,507 Members | 1,597 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,507 developers. It's quick & easy.

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 7791
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Eric-Sebastien Lachance | last post: by
5 posts views Thread by johnsuth | last post: by
11 posts views Thread by jesdynf | last post: by
2 posts views Thread by ejrfigueiredo | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.