I am learning to code JavaScript. In this script, I want a link to not go to its specified location, but to change the text in the paragraph before it. I have created a function to achieve it onclick, but the function is not able to return false.
<html>
<head>
<title>Javascript Test #6</title>
<script type="text/javascript">
document.getElementById("links").onclick=writeData();
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html"> Click here to change the paragraph content </a>
</body>
</html>
Don’t use the brackets. They force your function to evaluate. but you want to assign the function.
EDIT:
Of Course, you need to Assign your Function after the Document has been loaded. The way it is right now, there will be no element with id
linkswhen the assignment is executed. Basically, just put your script block below thelinkselement.EDIT (Using window.onload):