Skip to content Skip to sidebar Skip to footer

How To Use An Input Field As Query Parameter To A Destination?

For my sample Google Chrome extension, I have a textbox and a link:

Solution 1:

Solution 2:

UPDATED SOLUTION:

Here's an example with inline javascript that sets the href of the link appropriately and then lets normal processing handle the redirect (added encodeURIComponent base on Marcel's comment).

<input id="textbox"type="text" value="" size="25" />
<a class="button"id="googleLink" href="willBeChanged" 
 onclick="this.href='http://www.google.com/search?q=' + encodeURIComponent(document.getElementById('textbox').value);">
  <span>Search</span>
</a>

Solution 3:

Use JQuery to make your life easy.

Your JQuery would look like this:

$("#linky").attr('href',"http://www.google.com/search?q="+$("#textbox").val());

Your HTML would look like this:

<input id="textbox"type="text" value="" size="25"></input>
<a class="button" href="www.google.com"id="linky"><span>Search</span></a>

Solution 4:

Using jquery:

<scripttype="text/javascript">
    $(document).ready(function(){   
   $("#textbox").keyup(function () {
  var searchstring = $(this).val();
  searchstring = encodeURIComponent(searchstring);

  $('#searchlink').attr('href', 'http://www.google.com/search?q=' + searchstring);
  });

   });

HTML markup:

   <input id="textbox"type="text"  size="25">
   <a class="button" href="http://www.google.com"id='searchlink'>Search</a>

Post a Comment for "How To Use An Input Field As Query Parameter To A Destination?"