Hi all
I am new to Wix and was hoping I could get some help with coding my page correctly.
I have a user input box called "#input1" and a button called "#button1".
Each of my users will have a unique five digit code. These codes will lead to either one of two pages - "/page-A" or "/page-B" on my website. If the wrong code is entered, I want the code to lead to "/page-fail".
How can I make my code such that depending on what code is entered, the button redirects to the correct page?
For example - assume the codes should go to the following page:
11111 - PAGE-A (i.e. www.mywebsite.com/page-a)
22222 - PAGE-A
33333 - PAGE-B
44444 - PAGE-A
55555 - PAGE-B
66666 - PAGE-B
77777 - PAGE-B
88888 - PAGE-A
99999 - PAGE-B
[Other] - PAGE-FAIL
I've started making my code below based on everything i've seen on the site - but this is missing a few elements:
A) When you type in the code in the box and press enter - nothing happens. I'm not quite sure how to achieve this.
B) More importantly, when I click the button, nothing happens.
C) I haven't been able to add multiple codes (I think I will have about 150!) - not sure if this is the easiest way to do it?
Can anyone help? I've tried looking through the forum and the API help but am getting lost.
// For full API documentation, including code examples, visit https://wix.to/94BuAAs
import wixLocation from 'wix-location';
// ...
export function button1_click(event, $w) {
if ($w('#input1') === '11111') {
wixLocation.to("/page-a")
} else {
//wixLocation.to("/page-fail")
}
}
Thanks both! i will play around and see where I get to. Thanks so much!!
Really appreciate the help.
Hi - thanks again for the response! I'm really sorry but i can't seem to get the code working with either the 'if-else' or the 'or' functions. Could someone please take a look at my code above and tell me where I am going wrong? I would really appreciate if someone could try to fix it. Thank you so much
I think the “Or” isn’t working properly. But it’s odd neither the enter button or button works. I think there’s something that’s stopping it working at all. Any ideas?
Hi - thanks for the above.
I've updated my code - but it's still not working. Are you able to take a look and let me know where i'm going wrong?
import wixLocation from 'wix-location' export function input1_keyPress(event, $w) { $w.onReady(function (button1_click(event, $w)) { $w("#input1").onKeyPress((event, $w) => { if (event.key === "Enter") { }}}} export function button1_click(event, $w) { if ($w('#input1') === '11111') { wixLocation.to("/page-a") OR if ($w('#input1') === '22222') { wixLocation.to("/page-a") OR or ($w('#input1') === '33333') { wixLocation.to("/page-b") } else { //wixLocation.to("/page-fail") } } }
Hi - thanks so much for the response. I've tried to follow that page and use AND functions to get multiple codes in but I still can't get it to work. Is there something wrong with the code above? Or, if it's not too much trouble, could you share the code that i should be using? Thank you in advance!
A) You need a keyPress event: https://www.wix.com/corvid/reference/$w.KeyboardEvent.html
B) Did you set this event on the element property? In addition to the code.
C) You can use "AND" with "IF" (Operator &&)