Narabhut
Narabhut

Reputation: 837

Redirect to another webpage in jQuery via button click

I'm using Jade and my HTML template looks something like this

.container
        .row
          form
            .span3.input-append(style='margin-top: 30%; margin-left: 30%;')
              input#main_search.span2(style='height: 26px; width: 400px;', type='text', id='searchBox' )
              input.btn.btn-large.btn-primary(type='button', value='search', id='searchButton')
include partials/scripts

And my scripts.jade includes a bunch of Javascript functions including

$(document).ready(function() {
    console.log('foo');
    $("searchButton").click(function() {
        console.log('sup');
            window.location.replace("http://stackoverflow.com");
    });
});

Console output -

foo

Upvotes: 0

Views: 3381

Answers (2)

PSL
PSL

Reputation: 123739

You are missing # in the id selector. it should be #searchButton instead of searchButton

 $("#searchButton").click(function() {
        console.log('sup');
            window.location.replace("http://stackoverflow.com");
    });

Also not that even you have incorrect selector $("searchButton") which does not find any elements jquery will not throw any error while registering the click handler on them unlike document.getElementById where the result will be undefined and accessing the properties will throw an error.

Upvotes: 3

ClarkeyBoy
ClarkeyBoy

Reputation: 5010

Change searchButton to #searchButton. It's an ID, so must be prefixed with #.

Upvotes: 5

Related Questions