Consider the following code :
<html>
<head>
</head>
<body>
<script>
function showAlert (text) {
alert(text);
}
x = document.getElementById('aTag');
x.setAttribute('onclick', "alert('Hello World')"); //WORKS
x.setAttribute('onclick', "showAlert('Hello World')"); //DNW (showAlert is not defined)
</script>
<table>
<tr>
<td><a href="javascript:void(0)" id="aTag">TEST</a></td>
</tr>
</table>
</body>
</html>
The first onclick assignment works and produces a hyperlink on which, when you click on it, opens a dialog box with the Hello World
message. Probably because the window.alert
method is global (I don't know if it is the right word).
But the second assignment only returns the following error when clicked on :
showAlert is not defined
Being a beginner at JS, I think the issue might be about the showAlert
function scope or maybe the elementNode.setAttribute
method context in which the showAlert
function doesn't exist.
I thank you all in advance for your time.
Regards.