简体   繁体   中英

javascript getting the value of a text box

I have this text box here...

<input name="search" type="text" maxlength="512" id="search" class="searchField" autocomplete="off" title="" />

and I also have this submit

<input type="submit" name="btnSearch" value="Search" onclick="location.href='http://www.website.com/search/';" id="btnSearch" class="buttonSearch" />

what I am trying to do is add whatever is in the text box in my

onclick="location.href='http://www.website.com/search/';"

so it would look like this..

onclick="location.href='http://www.website.com/search/what ever the user searches';"

how would I go about doing this, I have been googling my little heart out.

Please avoid mixing JavaScript and HTML. You can remove onclick attribute and replace it with this in plain JavaScript somewhere after the DOM has loaded:

document.getElementById('btnSearch').onclick = function() {
    var search = document.getElementById('search').value;
    var searchEncoded = encodeURIComponent(search);
    window.location.url = "http://www.website.com/search/" + searchEncoded;
}

Also remember about escaping the search box, eg using encodeURIComponent() . Here is a working jsfiddle example .

This should work:

onclick="location.href='http://www.website.com/search/'+document.getElementById('search').value;"

But I wouldn't ever write that in one of my project as writing script directly on tags is a bad practice.

Here is a working jsfiddle

I moved the event handler out of the button as it is more maintainable. Also I encode the search query so that it gets to the server properly.

var search = document.getElementById('search');
var submit = document.getElementById('btnSearch');

submit.addEventListener('click', function(e) {
    var searchValue = encodeURIComponent(search.value);  // encode the search query
    window.location.href = 'http://www.website.com/search/' + searchValue ;
});

You can add it to the onclick event like so

document.getEelementById("btnSearch").onclick = function(){
    location.href='http://www.website.com/search/' + document.getEelementById("search").value;
} 

edit: aaaaand too slow... oh well. At least this is not inline.

You would be better off using the < script> tag for this task. Example:

<input name="search" type="text" maxlength="512" id="search" class="searchField" autocomplete="off" title="" />
...
<input type="submit" name="btnSearch" value="Search"  id="btnSearch" class="buttonSearch" />
<script type="text/javascript">
var button= document.getElementById('btnSearch');
button.onclick= function(){
    var text= document.getElementById('search').value;
    location.href='http://www.website.com/search/'+text;
}
</script>

However, you should try to 'clean' a little the text from the textbox so when you append it to the url you get a valid url. You should trim the text, then search for special characters and escape them, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM