0

I have a search button in html and I would like it link to

/home/search/search_term 

where the search term is the value entered into the corresponding input element.

Something like

<input id="input_field"> </input>
<button onclick="location.href='home/search/$(input_field).value'">Search</button>

would be ideal.

How can I achieve something like this?

1
  • 2
    are you using jQuery, if so add a tag of that. Commented Aug 14, 2014 at 14:16

2 Answers 2

2

There's no </input>. Change your code to:

<input id="input_field" />
<button onclick="location.href='/home/search/' + $('#input_field').val();">Search</button>

If you are not using jQuery, change the code to:

<input id="input_field" />
<button onclick="location.href='/home/search/' + document.getElementById('input_field').value;">Search</button>
Sign up to request clarification or add additional context in comments.

1 Comment

Consider escaping the input value - location.href='/home/search/' + encodeURIComponent(document.getElementById('input_field').value);
2

Firstly, input elements are self-closing. This means you use <input ... /> rather than <input>...</input>.

Once you've changed that, assuming you have the jQuery library included, you'd need to change:

'home/search/$(input_field).value'

To:

'home/search/' + $('#input_field').val()

If you don't have the jQuery library included this will not work, as the above snippet includes jQuery code. Instead you can use this:

'home/search/' + document.getElementById('input_field').value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.