I know this thread is old, but I was looking for a solution to this today. When you're "inlining" JavaScript like below, don't include "function xx()" and such. See the below example. I tested it today, and it works perfectly without console errors or warnings.
Wanted to share a solution:
<a href="#" type="button" onClick="var myDiv = document.getElementById('myDIV'); if (myDiv.style.display === 'block') { myDiv.style.display = 'none'; } else { myDiv.style.display = 'block'; }">click</a>
In a standard HTML markup, the following would be a typical implementation of the "inlined JS" example above.
<body>
<a href="#" type="button" onclick="myFunction()">Try it</a>
...(this is where the rest of the html page content goes)....
<script>
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
</body>
doSomething
multiple times? Declare it once. If you need to associate certain data with the<a>
, use adata-*
attribute and render information there. And don't set theonclick
in the tag - wait for the DOM to be ready, get all<a>
elements, and bind a click handler to each, callingdoSomething
and passing it thedata-*
attribute you may need.