ghost_king
ghost_king

Reputation: 930

TextBox and Button JS Onclick

I have a textbox. I wanted to use a JS onclick to append the input in the textbox to the "btnGO" button as below but it's not working:

document.getElementById('btnGo').onclick = function() {
  var search = document.getElementById('dlrNum').value;
  window.location.url = "http://consumerlending/app/indirect/dealerComments.aspx?dlrNum=" + search;
}
<input id="dlrNum" type="text" name="dlrNum" autocompletetype="disabled" class="ui-autocomplete-input" autocomplete="off">
<input id="btnGo" type="submit" value="GO" name="GO" runat="server">

What could I be missing?

Upvotes: 0

Views: 126

Answers (2)

johnniebenson
johnniebenson

Reputation: 540

Update window.location.url to window.location:

window.location = "http://consumerlending/app/indirect/dealerComments.aspx?dlrNum=" + search;

Upvotes: 1

Dekel
Dekel

Reputation: 62676

You had several problems there: 1. Your <input> elements are probably part of a form, so when you click on the submit button - the form will submit, unless you prevent it. 2. You need to use window.location.href (and not .url).

Here is the fix to your code:

document.getElementById('btnGo').onclick = function(e) {
    e.preventDefault()
    var search = document.getElementById('dlrNum').value;
    window.location.href = "http://consumerlending/app/indirect/dealerComments.aspx?dlrNum=" + search;
}

Note the e element inside the function(e) - it's there so we can use the event object to prevent the default behavior of the form submission.

Upvotes: 4

Related Questions