0

I have an autocomplete page which gives me a text value, but I want the same text value to open a webpage when hitting enter or selecting the suggested value.

E.g. when I search for india, I type "ind" and "india" automatically comes up, but and I want "india" to open a webpage (like "domain.com/india") when selecting the value or hitting enter

The code is as follows:

<datalist id="countries">
    <select>
        <select id="dynamic-select">
        <option value="www.blahblah.com">Blah</option>
        <option value="www.something.com">something</option>
<script>
    $('#dynamic-select').bind('change', function () { // bind change event to select
        var url = $(this).val(); // get selected value
        if (url != '') { // require a URL
            window.location = url; // redirect
        }
        return false;
    });
</script>
</datalist>

It pulls out the value but it doesn't open a webpage for me. Can someone help me with this please?

kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27

2 Answers2

0

You need to use correct URL syntax. You're missing the // prefix, so it's treating the URL as a filename on your website, not the address of another website.

$('#dynamic-select').bind('change', function() { // bind change event to select
  var url = $(this).val(); // get selected value
  if (url != '') { // require a URL
    window.location = url; // redirect
  }
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dynamic-select">
  <option value="">Please choose</option>
  <option value="//www.blahblah.com">Blah</option>
  <option value="//www.something.com">something</option>
</select>
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Change

window.location = url; 

To

window.location.href= url; 
Rahul Kumar
  • 99
  • 3
  • 10
  • They're equivalent: http://stackoverflow.com/questions/2383401/javascript-setting-location-href-versus-location – Barmar Aug 26 '15 at 21:44