Javascript/Jquery to go to URL from <input> and <datalist> -


my problem: looking make input box autocompletes suggestions type them in. upon typing them taking first selection (this figured out in plug-in) either clicking or pressing enter, i'd submit selection tied url redirect new url.

basic example of plug-in

this here directly developer's website , example of used.

<input class="form-control awesomplete" list="mylist" />  <datalist id="mylist">  	<option>ada</option>  	<option>java</option>  	<option>javascript</option>  	<option>brainfuck</option>  	<option>lolcode</option>  	<option>node.js</option>  	<option>ruby on rails</option>  </datalist>

the basic changes

what use navigate list of u.s. states. idea here redirect new (or current window) url associated state. alabama going http://www.alabama.gov, , on.

<input class="form-control awesomplete" list="states" />  <datalist id="states">  	<option>alabama</option>  	<option>alaska</option>  	<option>arizona</option>  	<option>arkansas</option>  	<option>california</option>  	<option>colorado</option>  	<option>connecticut</option>  </datalist>

i stuck here: 

after going through many searches , seeing jquery or javascript required this, i've tried go through solutions, cannot quite seem make work. might not possible. didn't want throw in many examples of tried , clutter post up, tried leave in basic form idea in mind.

as far know, i'd need tie url value option tag, correct? have examples of in code, once again, tried leave in basic form viewer.

you store url in value property, , read out when input made:

var aweinput = new awesomplete(myinput);  myinput.addeventlistener('awesomplete-select', function(e) {    var url = e.text.value; // value associated selection    console.log('navigating to: ' + url)    // optional actions:    e.preventdefault(); // prevent url appearing in input box    e.target.value = e.text.label; // set value selected label    aweinput.close(); // close drop-down    //window.location.href = url;  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/awesomplete/1.1.2/awesomplete.js"></script>  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/awesomplete/1.1.2/awesomplete.css" />  <input id="myinput" list="states" />  <datalist id="states">  	<option value="http://www.alabama.gov/">alabama</option>  	<option value="http://alaska.gov/">alaska</option>  	<option value="https://az.gov/">arizona</option>  	<option value="http://www.arkansas.gov/">arkansas</option>  	<option value="http://www.ca.gov/">california</option>  	<option value="https://www.colorado.gov/">colorado</option>  	<option value="http://portal.ct.gov/">connecticut</option>  </datalist>


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -