Redirect to a page based on a text input?

Hi, i’m trying to make a simple code that redirect you based of the input.

For example: If i write “hello” in the Page name field, clicking the button that says “Go!” will redirect me automaticly on the page with the name “Hello”. I Want to make this system automatic, Thank you!

This is my page:

For this, you should need the respective pages.
Like, when the user types something like ‘hello’ there should be a page ‘hello’ otherwise it would return an undefined page…

(Not tested)
If it works let me know, also let me know if it don’t works also !

#input1 - the input where the user types
#goButton - the button

import wixLocation from 'wix-location';

//put this code (below code) under the $w.onReady() function
$w('#goButton').onClick((event) => {
 if ($w('#input1').valid) {
  let toLocation = $w('#input1').value;
  wixLocation.to(`/${toLocation}`);
  }
 });

This also can be helpful →
https://www.wix.com/corvid/reference/wix-location/queryparams

Thank youu!! It works! :smiley:

Can i add an else that shows an error message?

@dragonfx1234 Do you want to show an error message if the input is not valid ?
If it is →

if ($w('#input1').valid) {
  let toLocation = $w('#input1').value;
  wixLocation.to(`/${toLocation}`);
  }
  else {
   console.log("Input not valid !!!");
  }

@dragonfx1234 Or do you want to show an error message if the page does not exist ?
If it is →
You should store all your page url (Check the article) in an array…

let links = ["home", "about", "page1", "page2"];

Then →

if ($w('#input1').valid) {
  var isValid = links.indexOf($w('#input1').value); 
  if (isValid) {
   let toLocation = $w('#input1').value;
   wixLocation.to(`/${toLocation}`);
   } 
   else {
    console.log("Page does not exist !!!");
   }
  }

JavaScript Array indexOf() Method

@ajithkrr I Want to show a text if the page doesn’t exist and not that redirect to the default 404 :smiley:

@dragonfx1234 This works →
You should store all your page url (Check the article) in an array…

let links = ["home", "about", "page1", "page2"];

Then →

if ($w('#input1').valid) {
  var isValid = links.indexOf($w('#input1').value); 
  if (isValid) {
   let toLocation = $w('#input1').value;
   wixLocation.to(`/${toLocation}`);
   } 
   else {
    console.log("Page does not exist !!!");
   }
  }

JavaScript Array indexOf() Method

@ajithkrr Ok! Thank you so much!

Hi! I found this thread because I want to do the same thing, but I’m still having trouble. Using the code from @ajithkrr where do I put the actual valid input and actual page destination? For example, let’s say I want the user to type OPEN and if that’s correct, it takes the to the page NEW ROOM. How would that look exactly? Thanks!