How do I replace the “0” in the following line with a variable?
$w("#slideshow1").changeSlide(0);
I’ve tried a couple of different things, but I keep getting correction marks. Is there a way to substitute a variable for the zero in the code snippet above?
Yes, this works. I had something similar working earlier when I was testing, but I need one more step. Here’s what I had earlier while testing:
var galImage = 0;
$w.onReady(function () {
$w("#slideshow1").changeSlide(galImage);
});
But I need to use a session variable instead. I “set” the session variable with a button-click on my page and then “get” the session variable in the lightbox where I placed my slideshow. I the following snippet, I change out “var galImage = 0;” for a “getItem” variable declaration:
import {session} from 'wix-storage';
var galImage = session.getItem("galImage");
$w.onReady(function () {
$w("#slideshow1").changeSlide(galImage);
});
The session variable is an integer. All should be well, but I’m getting a correction mark under “galImage” in the parentheses in the “chanegeSlide” option.
I’ve also tried this (using “let” in the “onReady” function) instead of “var”:
import {session} from 'wix-storage';
$w.onReady(function () {
let galImage = session.getItem("galImage");
$w("#slideshow1").changeSlide(galImage);
});
But the value has been set. I set the stored session value on the actual webpage. The lightbox then gets the stored value, which is then used to change slides in the slider in the lightbox.
As a test, I created a variable that gets the value stored in session storage. I then reset the session storage value using the created variable. This is redundant, of course, but I did this so that “set” and “get” are in the same body of code. Then, I created yet another variable called “num,” based on the reset session variable.
Still doesn’t work.
The “changeSlide” operation doesn’t seem to like it when the value of the variable its using is passed from storage.
Not sure what to do next. Here’s the test code described above:
import {session} from 'wix-storage';
var galImage = session.getItem("galImage"); // variable created from page storage
$w.onReady(function () {
session.setItem("galImage", galImage); // stored variable reset (set/get same page)
let num = session.getItem("galImage"); // new variable from reset storage
$w("#slideshow1").changeSlide(num); // correction mark under "num" (!shown here)
});
This is silly, I know. All I’m doing is trying to have the “set” and “get” in the same body of code to test whether or not the “changeSlide” operator simply doesn’t like values passed from storage. It appears to be the case.
Perhaps I’m missing something. Figuring this out will save many lines of code.
In short, I need to be able to set/store an integer from a button on my webpage. This button also triggers a lightbox containing a slideshow. The code in the lightbox must then retrieve the stored integer, the value of which is used to change slides in the slideshow.
Is there another way to get/pass this value/integer without adding a collection?
I had this working, but I was using if statements. I could also use a case statement. However, in either case we’re talking a lot of extra code. Passing the integer simplifies things a lot, resulting in many fewer lines of code.
Lastly, I simply set a new session variable. Then, I “let” a variable to get the stored value and then put this value in the parentheses of the “changeSlide” operator. . .
Didn’t work.
There must be a way around this. I ran into this problem with indexes in the Pro Gallery. You can retrieve items in an index, but you can change the index your viewing in the Pro Gallery by any coded means. Hence my use of a slide show.
Again, I can get it to work. But there’s just so much extra, unnecessary code.
The answer is to use the “parseInt()” global method.
I learned that the problem was indeed that session variables are not passing as expected. Though I “set” my variable as an integer on my page via button-click, when I then “get” the variable in my lightbox, the value of the variable passes as a string. You can’t change the slide in a slider box with a string. The “parseInt()” global method turns the string back into an integer.
Note the following code:
import {session} from 'wix-storage';
$w.onReady(function () {
let slide = parseInt(session.getItem("galImage"));
$w("#slideshow1").changeSlide(slide);
});
Originally, I had several line of code, because I had to use an if statement to condition the string and result a corresponding integer. By the time my project is complete, I would have had a few hundred lines at least. But the code snippet above is all I’ll need now. Just these few lines.
How sweet is that?
Thank you for your help, man. You may not have solved the issue, but you certainly helped me ask the right forum question in my very next post.