0

I'm currently practicing my skills in node.js with a movie app project and I'm stuck with this (I hope) small problem.

I have a search input element on the homepage that would accept a movie title. After pressing the submit button I would like to redirect to a search page with the results.

This is the code in my js file on the client side:

   $(function () {

    $('button').click(function (event) {

        // the value people enter in the search box
        var search = $('.searchInput').val();

        //replace spaces with _
        var res = search.replace(/\s/, "_");

        // redirect to trigger GET in indexjs with specific search value
        window.location.replace("localhost:4000/search/" + res);

        });
});

And I would like to trigger this GET in my index.js (server side) file to start firing an ejs file specific to the search value.

router.get('/search/:id', function (req, res, next) {
    console.log('entered')
    var name = req.params.id
    res.render('search', {
        name:name});
});

The window.location.replace function unfortunately isn't working though. Can anyone see what the problem is?

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
Tijl Declerck
  • 105
  • 1
  • 11
  • No errors in the console? – Roy Berris Sep 19 '17 at 12:54
  • You miss a protocol http:// or https:// - if you are already on localhost:4000, do location.replace("/search/"+res) – mplungjan Sep 19 '17 at 12:55
  • $('.searchInput') returns an array first of all. What does your path look like after appending the query to URL? You can also use $(location).attr('href',"localhost:4000/search/" + res); since you already have JQuery. You're also missing the protocol , see above. – Adrian Sep 19 '17 at 12:55
  • `After pressing the submit button` If you have this inside a form tag, it will just reload the current page. try putting a `event.preventDefault()` to stop the default behaviour of a submit button. – Keith Sep 19 '17 at 12:57
  • you can use window.location.href="your_url_here"; – Kunvar Singh Sep 19 '17 at 13:01
  • @mplungjan You're right. My bad. – Adrian Sep 19 '17 at 13:01
  • Keith's answer was what was wrong with it. I also added the protocol like you guys said so that probably did the trick too. Thanks a lot guys, you saved me a lot of time. Have a great day. – Tijl Declerck Sep 19 '17 at 13:12

0 Answers0