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 5993991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:45:14+00:00 2026-05-22T23:45:14+00:00

I have a link that looks like this: <a id=mylink onclick=deleteHike( 3 ); href=javascript:void(0);>Yes</a>

  • 0

I have a link that looks like this:

<a id="mylink" onclick="deleteHike( 3 );" href="javascript:void(0);">Yes</a>

It is able to call this JavaScript:

window.onload = function()
{

  //Get a reference to the link on the page
  // with an id of "mylink"
  var a = document.getElementById("mylink");

  //Set code to run when the link is clicked
  // by assigning a function to "onclick"
  a.onclick = function( hike_id )
  {
     // Somecode her
     // But when I try to use the hike_id it displays as [object MouseEvent] 
  }
}

But the value that comes in is [object MouseEvent], not the number that I was expecting. Any idea why this happens and how to fix this? 🙂

Thanks!

  • 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-05-22T23:45:15+00:00Added an answer on May 22, 2026 at 11:45 pm

    You are trying to assign the function to your link in two different and conflicting ways.

    Using the eval-ed function string, onclick = "function(value)", works but is deprecated.

    The other way of binding the click handler in the onload event works too, but if you want a particular value to be passed, you’ll have to change your script a bit because the value as given in the initial onclick is completely lost when you set the onclick to a new function.

    To make your current method work, you don’t need an onload handler at all. You just need this:

    function deleteHike(hike_id) {
       // Some code here
    }
    

    To do it the second way, which I recommend, it would look like this:

    <a id="mylink" href="javascript:void(0);">Yes</a>
    

    with this script:

    function deleteHike(e, hike_id) {
       // Some code here
       // e refers to the event object which you can do nifty things with like
       //  - learn the actual clicked element if it was a parent or child of the `this` element
       //  - stop the event from bubbling up to parent items
       //  - stop the event from being captured by child items
       //   (I may have these last two switched)
    }
    
    function getCall(fn, param) {
       return function(e) {
          e = e || window.event;
          e.preventDefault(); // this might let you use real URLs instead of void(0)
          fn(e, param);
       };
    }
    
    window.onload = function() {
       var a = document.getElementById("mylink");
       a.onclick = getCall(deleteHike, 3);
    };
    

    The parameter of a DOM event function is the event object (in Firefox and other standards-compliant browsers). It is nothing in IE (thus the need to also grab window.event). I added a little helper function for you that creates a closure around your parameter value. You could do that each time yourself but it would be a pain. The important part is that getCall is a function that returns a function, and it is this returned function that gets called when you click on the element.

    Finally, I recommend strongly that instead of all this, you use a library such as jQuery because it solves all sorts of problems for you and you don’t have to know crazy JavaScript that takes much expertise to get just right, problems such as:

    • Having multiple handlers for a single event
    • Running JavaScript as soon as possible before the onload event fires with the simulated event ready. For example, maybe an image is still downloading but you want to put the focus on a control before the user tries to use the page, you can’t do that with onload and it is a really hard problem to solve cross-browser.
    • Dealing with how the event object is being passed
    • Figuring out all the different ways that browsers handle things like event propagation and getting the clicked item and so on.

    Note: in your click handler you can just use the this event which will have the clicked element in it. This could be really powerful for you, because instead of having to encode which item it was in the JavaScript for each element’s onclick event, you can simply bind the same handler to all your items and get its value from the element. This is better because it lets you encode the information about the element only in the element, rather than in the element and the JavaScript.

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

Sidebar

Related Questions

I have a link that looks like this: <a href=# onclick=new Ajax.Request('/data_entry/ajax_add_term/131?contract_id=227', {asynchronous:true, evalScripts:true});
I have a CSS link that looks like this: <link href=../../css/WW/parts.css type=text/css rel=stylesheet />
I have link that calls a function when clicked: <a href=javascript:spawnMenu(this); id=link1>Test1</a> To make
I currently have a login link on my application that looks something like this:
I have a link being generated that looks like so: <a target=_blank title=Test href=file:///c:/test.xls>Test</a>
Say you have a link with a relative path that looks like this: <a
I have a page of things that looks like this (and yes, I have
I have a link that looks like this: <p class=half_text> <?php echo $upvotes; ?>
I have a link that looks like this: <p class=half_text><?php echo $upvotes; ?> <strong>
At this URL boiseresturants.com/mexican/baja-fresh-mexican-grill.html I have an anchor link that looks like <a href=#takemehere>his

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.