Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 17th, 2005, 04:08 PM
Registered User
 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default onClick and go to anchor #something

I seem to have a problem: I have a button that runs a js function, and I want it to work so that when you press the button, it runs the function (collapse a table) and scrolls to an anchor #something.

<a href="#something" onclick="return toggle_collapse('1200')"><img src="images/button.gif" alt="button" border="0" /></a>

But this doesn't work.. when i click it, it only collapses the table but doesnt move to the <div id="something> labeled location.

Does this have anything to do with the code of the func itself, or is there a workaround?

 
Old January 17th, 2005, 04:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

possible, can you post your Javascript function ?
 
Old January 17th, 2005, 04:44 PM
Registered User
 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

its a vbulletin function.. I posted it on their website, noone responded.

this is the function
Code:
// #############################################################################
// function to toggle the collapse state of an object, and save to a cookie
function toggle_collapse(objid)
{
    if (!is_regexp)
    {
        return false;
    }

    obj = fetch_object("collapseobj_" + objid);    
    img = fetch_object("collapseimg_" + objid);
    cel = fetch_object("collapsecel_" + objid);

    if (!obj)
    {
        // nothing to collapse!
        if (img)
        {
            // hide the clicky image if there is one
            img.style.display = "none";
        }
        return false;
    }

    if (obj.style.display == "none")
    {
        obj.style.display = "";
        save_collapsed(objid, false);
        if (img)
        {
            img_re = new RegExp("_collapsed\\.gif$");
            img.src = img.src.replace(img_re, '.gif');
        }
        if (cel)
        {
            cel_re = new RegExp("^(thead|tcat)(_collapsed)$");
            cel.className = cel.className.replace(cel_re, '$1');
        }
    }
    else
    {
        obj.style.display = "none";
        save_collapsed(objid, true);
        if (img)
        {
            img_re = new RegExp("\\.gif$");
            img.src = img.src.replace(img_re, '_collapsed.gif');
        }
        if (cel)
        {
            cel_re = new RegExp("^(thead|tcat)$");
            cel.className = cel.className.replace(cel_re, '$1_collapsed');
        }
    }
    return false;
}
 
Old January 17th, 2005, 05:27 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

all you need to do is the work out what your anchor is for each element and then put that value into a variable and then do something like

var anchorRef="something"; // calculated value

document.location=document.location + "#" + anchorRef;
 
Old January 17th, 2005, 05:42 PM
Registered User
 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't really understand.. where do i put this. And also.. the location of the anchor can change on different pages, since vbulletin uses mysql and php. I just need to know if there is a way to go to that anchor without using href="#something".

Like
Code:
<a href="#" onClick="return toggle_collapse('15150'); anchor(#something)">
or something of that sort

 
Old January 17th, 2005, 07:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

something like :

<a href="#" onClick="return toggle_collapse('15150'); document.location=document.location+'#something';" >

may work.
 
Old January 18th, 2005, 06:16 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can also set the location.hash property directly to the anchor's name.

--

Joe (Microsoft MVP - XML)
 
Old November 28th, 2006, 06:45 AM
Registered User
 
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Don't listen to their bullshi t. Just remove "return false;" in the javascript code would do the job.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Anchor Tag kapilmirchi ASP.NET 1.0 and 1.1 Basics 2 November 18th, 2008 05:57 AM
anchor and get variables. SignalFade Beginning PHP 4 January 19th, 2006 08:56 PM
Javascript in Anchor Tag jamara JSP Basics 1 May 30th, 2005 01:00 AM
Anchor Tags and the onClick event Blaise Javascript 4 June 16th, 2003 04:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.