Results 1 to 9 of 9

Thread: onclick in a link

  1. #1
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default onclick in a link

    I have this onclick in a link that calls a javascript function. This function holds two variables: 1) a 1 or -1 indicating a direction and 2) and a number representing the number of a picture to display. My problem is that I need to load a new number into that function when ever the next button is clicked. Here is what I have:

    HTML Code:
    <a href = "#" id = "next" onClick = "processClick(1, 1);"><span>CSS2</span></a>
    and the javascript looks like:

    Code:
    var next = document.getElementById('next');
    next.onclick = '"processClick(1, 1);"';
    The idea is that I want the onlclick attribute to have the old function call replaced with the new one when that code executes. However, next.onclick returns an [Object] and not a string value. How do I get at the string value for the onclick attribute? Thanks,

    -Shandy

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    onclick is a null function. when you set it your actually changing it, so it will always return an object. so you would use
    Code:
    var next = document.getElementById('next');
    next.onclick = processClick(1, 1);
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default onclick

    I did try that, but what seems to happen is that that function gets executed insted of just setting the attribute for the link to that value. Am I wrong in assuming this is what is happening, or is possible I have a bug in my code?

  4. #4
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    try
    Code:
    var next = document.getElementById('next');
    next.setAttribute("onclick", "processClick(1, 1)");
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  5. #5
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Kind working

    That seems to set the attribute correctly but when I try and go next again my script doesn't work. I am playing around with a picture gallery that allows you to hit the next (or the prev) button to view more pictures. It goes from the first to the second picture fine, but I think that is because the values are hard coded, but it won't gone from the second to the third. When I put an alert on the function calls my next.onlick says:

    Code:
    function anonymous()
    {
       processClick(1, 1);
    }
    But after I try and re-set the onlcik value on the link, my alert just says:

    "processClick(1,3);"

    So it is setting the value correctly but I get the feeling something has changed from how the function is interrupted by javaScript.

    Hope I made this clear and not to confussing.

    -S

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Events need to be functions:

    Code:
    var next = document.getElementById('next');
    next.onclick = function(){processClick(1, 3);};
    You can even use variables, but it's a little tricky, as they must be declared in the same scope as the function. I'm not sure if you need to do this, or if you can just use the raw number, as I don't see the rest of your code. There are simpler ways to do this in most cases though, rather than passing the number to a new function, it could be kept track of elsewhere, and used in a static function.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    That worked perfectly. One thing though is that the directory where my web pages resides keeps opening evertime I hit the next button - which fires off the javsScript. Is that related to the javaScript or am I just loosing my mind. Thanks for all the help,

    -S

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's probably because of the way your link is setup, there are several ways to deal with this if it is the issue. I like some of them, here's a fairly good one, change the HTML to:

    Code:
    <a href = "javascript:next();" id = "next" onClick = "processClick(1, 1);return false;"><span>CSS2</span></a>
    And change the function update to:

    Code:
    var next = document.getElementById('next');
    next.onclick = function(){processClick(1, 3);return false;};
    Notes: At this point, the href value can now be anything you like because it will not fire when javascript is enabled. However, using a phony javascript call for the href will prevent it from firing when javascript is disabled. The phony call, if syntactically correct, may also be descriptive - a good idea, as it will show in the status bar on hover in many browsers. Alternatively, the href may be to a page - a fall back page for non-javascript users. The choice depends upon what you are trying to do, and upon what would be most helpful to your users, the javascript enabled and the javascript disabled.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    that looks to have solved all my problems, thanks.

    -S

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •