Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 clueless, but would really appreciate some help, thank you in advance!

<html>

<head>
</head>

<body>
  <script type="text/javascript">
    function link() {
      var link_s = document.getElementById('link_id').value;
      document.getElementById('link_str').innerHTML = link_s.link()
    }
  </script>

  <h2> Search box </h2>
  <input type='text' id='link_id'>
  <input type='button' id='link_str' value='Search' onClick='link()'>
</body>
<html>
like image 453
Samir Avatar asked Mar 22 '23 20:03

Samir


1 Answers

Try this JavaScript:

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

and change the onclick in the HTML:

<input type='text' id='link_id'>
<input type='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;
};
like image 120
Alex W Avatar answered Mar 25 '23 11:03

Alex W