Simple question:
I have a button and he’s already associated with an anchor point and an export function.
I would like to know if it is possible to go back to the previous anchor point if the user clicks in that button again?
Below is the my current page code.
Cheers!
$w.onReady( function () {
});
export function button1_click() {
if ($w(‘#box18’).collapsed) {
$w(‘#box18’).expand();
}
else
$w(‘#box18’).collapse();
}
export function button2_click() {
if ($w(‘#box1’).collapsed) {
$w(‘#box1’).expand();
}
else
$w(‘#box1’).collapse();
}
![](https://us1.discourse-cdn.com/wix/original/3X/a/2/a2fcc92f79507a48a1fa5d77c818cf0162cda434.png)
Hello Raniere,
If you’re looking to do something like this: https://qumseyamajd.wixsite.com/testing/anchor-session
Here is the code:
import wixLocation from 'wix-location';
let x = 1; //Flag to determine which to go to, you can also store this in session
$w.onReady(function () {
//TODO: write your page related code here...
});
//Checks flag to see which anchor it should go to
export function button1_click(event, $w) {
if(x === 0) {
wixLocation.to('/anchor-session#anchor1'); //Linking to anchor with wixLocation API
console.log("anchor1");
x = 1;
} else {
wixLocation.to('/anchor-session#anchor3');
console.log("anchor3");
x = 0;
}
}
Explanation:
I keep a flag tracking which the last anchor that was directed to and set up a conditional statement to redirect to one of them based off of the flag.
wixLocation - here
PS. Make sure to unlike everything on your button so it doesn’t have any functionality by default.
Goodluck,
Majd
I’m trying this code but it 's not working:
import wixLocation from ‘wix-location’;
let x = 1;
$w.onReady( function () {
});
export function button1_click() {
if (x === 0) {
wixLocation.to(‘/anchor-session#anchor1’); //Linking to anchor with wixLocation API
console.log(“anchor1”);
x = 1;
} else {
wixLocation.to(‘/anchor-session#anchor3’);
console.log(“anchor3”);
x = 0;
}
if ($w(‘#gallery1’).collapsed) {
$w(‘#gallery1’).expand();
}
else
$w(‘#gallery1’).collapse();
if ($w(‘#gallery2’).expand) {
$w(‘#gallery2’).collapse();
}
}
I have 2 Anchor Points:
Anchor Point 1: On a particular page session.
Anchor Point 2: At the top of the page.
I would like to do this:
When the user clicks the button it is directed to Anchor Point 1 and when it clicks again was taken to Anchor Point 2.
Any suggestions?
Hello again,
Try changing the id’s in the code to match with the id’s of your element.
Best,
Majd