Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:23:58+00:00 2026-06-13T23:23:58+00:00

I have been looking online, but I have failed to find a direct answer

  • 0

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";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:23:59+00:00Added an answer on June 13, 2026 at 11:23 pm

    Although that should work, it’s better practice not to bind events inline. I would suggest looking into addEventListener and 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.

    window.onload = function(){
       //add any event listeners using the above methods in here
    }
    

    Example:

    <script>
        window.onload = function(){
          var t = document.getElementById("test");
          t.addEventListener("click", sayHi, false);
    
          function sayHi(){
           alert("Hi");
          }    
        }
    
    </script>    
    
    <div id="test">test</div>​
    

    According to your above echo statements, if you are determined to make it work that way then you can try this:

    echo "<script type='text/javascript' src='src/jquery/jquery.js'></script>\n";
    echo "<script>\n"
    echo "function counter(){\n";
    echo "alert('HELLO WORLD!');\n";
    echo "}\n";
    echo "</script>\n";
    echo "<a href='#' id ='loginbutton' onClick='counter()'></a>\n";
    

    notice that I closed the script tag including jQuery and added a new opening tag right below it.

    EDIT:

    Script tags that reference external resources (via the src attribute)
    are no longer able to execute script embedded within the tag itself.

    Read more here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking around online to try to find a complete solution but
I have been looking at paypal for a online transaction, but I wanted to
I have been looking online to find documentation for the function in QTP and
I have been looking at examples online, and tutorials, and I cannot find anything
I have been looking for an answer to this on Stack Overflow, but I
I have been looking online for some time now, but I still haven't figured
I have been looking online and in the android opencv source code, but I
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been looking into backbone.js and I can't seem to figure out how
I have been looking for help online on how to design my php classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.