Enter button code for Search

Hello, I have seen multiple posts regarding my query but I cannot find a way to get our search input text box “searchBar” to search when hitting the enter key… Any help would be gratley appreciated.

On our homepage there is a search box, users can type in their keyword which brings up the results on a different page (results).

Here is our code, if anyone can suggest how or where to add the code with an example it would be great!

import {local} from ‘wix-storage’;
import wixLocation from ‘wix-location’;

$w.onReady( function () {
});

export function searchButton_click_1() {
let word = $w(“#searchBar”).value;
local.setItem(“searchWord”, word);
wixLocation.to(/results);
}

Hello,

In order to implement this functionality you will need to use onKeyPress().
https://www.wix.com/corvid/reference/$w.TextInputMixin.html#onKeyPress

This issue was raised before, you can find the code snippets and instructions on how to implement it in the forum posts below:
https://www.wix.com/corvid/forum/community-discussion/input-onkeypress-function-working-only-after-clicking-away
https://www.wix.com/corvid/forum/corvid-tips-and-updates/give-the-textinput-onkeypress-function-some-time

Thanks for the response :slight_smile: I was aware I had to use onKeyPress but like I said… I can’t get it to work with my code and don’t know where to insert it. I have tried multiple times to input the code but I don’t know where to put it and do not want to destroy what I’ve already done. Is there any chance someone could show me an example with my code above. :tired_face:

export function yourElement_keyPress(event) {
if (event.key === "Enter") {
let word = $w("#searchBar").value; 
local.setItem("searchWord", word); 
wixLocation.to(`/results`);
}
}

Try this

Cheers Chris, but it’s not working. I inserted it at the bottom of the code and changed the “yourElement” to “searchBar”

import {local} from ‘wix-storage’;
import wixLocation from ‘wix-location’;

$w.onReady( function () {
});

export function searchButton_click_1() {
let word = $w(“#searchBar”).value;
local.setItem(“searchWord”, word);
wixLocation.to(/results);
}
export function searchBar_keyPress(event) {
if (event.key === “Enter”) {
let word = $w(“#searchBar”).value;
local.setItem(“searchWord”, word);
wixLocation.to(/results);
}
}

:sleepy:

does the search bar have a keypress an event set?

What does the console log say if the keypress is used?

Try this to see if the event is triggering

export function searchBar_keyPress(event) {
if (event.key === "Enter") {
console.log("SUCCESS")
} else {
console.log("ERROR")
console.log(event.key)
}
}

Make sure the event is set via the properties panel

Done it! Massive thanks to you both.

The search bar was enabled by default, but I noticied that the event onKeyPress didn’t have any blue writing inside it. When I clicked it it then added itself searchBar_KeyPress_1. Therefore I then changed Chris’s code to this:

export function searchBar_keyPress_1(event) {
if (event.key === “Enter”) {

let word = $w(“#searchBar”).value;
local.setItem(“searchWord”, word);;
wixLocation.to(/results);
}
}
)
wixLocation.to(/results);;
wixLocation.to(/results);
}
}
)
};
wixLocation.to(/results);
}
}
)
};
wixLocation.to(/results);
}
}
)

and it worked! :grinning: Thank you so much again!

No problem!
just a note, the ‘enabled by default’ just means that people can interact with the user input element (in this case a drop down) when the page loads.

You need to add the event via the properties panel (or code it independently) to allow the event to trigger.

Have a great day!