I have been looking online, but I have failed to find a direct answer to my question:
Can you use onClick for a tags?
In other words can you do this (note echoed through php)?
echo "<a href=\"#\" id =\"loginbutton\" onClick=\"counter()\"></a>\n";
I tried to run this, but when I click on the link, my counter function is not being called.
The rest of my code snippet:
echo "<script type=\"text/javascript\" src=\"src\jquery\jquery.js\">\n";
echo "function counter(){\n";
echo "alert(\"HELLO WORLD!\");\n";
echo "}\n";
echo "</script>\n";
echo "<a href=\"#\" id =\"loginbutton\" onClick=\"counter()\"></a>\n";
Although that should work, it’s better practice not to bind events inline. I would suggest looking into
addEventListenerand for older versions of IE,attachEvent. More information on these can be found in a topic here: Correct usage of addEventListener() / attachEvent()?If you wait for the window to be ready, you ensure that the element is on the page and defined for you to access it.
Example:
According to your above echo statements, if you are determined to make it work that way then you can try this:
notice that I closed the script tag including jQuery and added a new opening tag right below it.
EDIT:
Read more here