Search Text/Value in Google

Code to enable “onClick event” to search a clicked text or value on google.

Thanks for the help in advance!

More details are need. Do mean that you have a text in a text-box and you want that by clicking it you will be redirected to google.com with this text as a query? Do you meant that by click, you will stay in your domain but run this query using google custom search (in your site)? Something else?

Yes, want that by clicking it you will be redirected to google.com with this text as a query. The search result opens a another browser page (_blank) and remains on google.

@freddy ,
for text-box:

import wixLocation from 'wix-location';
$w.onReady(function () {
    $w('#text1').onClick((event) => {
    let query = $w('#text1').text;
        wixLocation.to('http://www.google.com/search?q=' + query);
    })
});
//here you can't control if it open in a new tab or not (it depends on the browser)

for button:

$w.onReady(function () {
    $w('#button1').onClick((event) => {
     let query = 'http://www.google.com/search?q=' + $w('#button1').label;
     $w('#button1').link = query;
    })
    /*no need to import wix-location.  Here you can use "_blank": https://www.wix.com/corvid/reference/$w.LinkableMixin.html#target
    */


Make sure the button is not linked via the editor, only by code.

@jonatandor35 Excellent JD. Thank you!