Skip to content Skip to sidebar Skip to footer

Simple Javascript Search Box

I tried connecting the first text box, so it would turn it into a URL, and then when 'search' is clicked, it would jump to that website, not sure if it's impossible, or i'm just cl

Solution 1:

Try this JavaScript:

functiongoTo()
{
    location.href = document.getElementById('link_id').value;
}

and change the onclick in the HTML:

<inputtype='text'id='link_id'>
<inputtype='button'id='link' value='Search' onClick='javascript:goTo()'>

Edit:

If you want to follow the unobtrusive JavaScript way, you would move the onclick completely into your JavaScript code, and remove it in the HTML:

document.getElementById('link').onclick = function()
{
    location.href = document.getElementById('link_id').value;
};

Solution 2:

if you want to create a form that will search google use this:

<scripttype="text/javascript">functiondos12(g1) {
window.open('https://www.google.com/#q='+g1 +" site:linkedin.com",      'G1window');
}

      </script><formonsubmit="dos12(this.g1.value); return false;"><inputtype="text"name="g1"size="20"placeholder="Name" /><inputtype="submit"value="Submit" />
    Search Linkedin via Google<br /><br /></form>

If you want to search a website then you will need to get the search string used. for example, if you want to search the ABN lookup site for Australia you would use the following code.

<scripttype="text/javascript">functiondos10(a1) {
window.open('http://abr.business.gov.au/SearchByName.aspx?SearchText=' + a1,   'a1window');
}

      </script><formonsubmit="dos10(this.a1.value); return false;"><inputtype="text"name="a1"size="20"placeholder="Name" /><inputtype="submit"value="Submit" />
    ABN Lookup name<br /><br /></form>

hope this helps. you don't need to add anything else. Just copy and paste this into notepad or your code editor and save as test.html then open with browser to test it.

Solution 3:

Here's how I would do it:

 <inputtype="text"id="link-box"/>
 <inputtype="button"id="search-button" value="Search" 
        onclick="window.location = document.getElementById('link-box').value;"/>

Of course you could do this:

<scripttype="text/javascript">functionfunc(){
           window.location = document.getElementById('link-box').value;
      }
 </script>

With onclick="func();"

Or

<scripttype="text/javascript">document.getElementById("search-button").onclick = function(){
            window.location = document.getElementById('link-box').value;  
       };        
 </script>

Or last of all

<scripttype="text/javascript">document.getElementById("search-button").addEventListener("click", function(){
            window.location = document.getElementById('link-box').value;
       });
 </script>

Post a Comment for "Simple Javascript Search Box"