Here are code examples of my actual scenario. I need to call typeahead only when clicking once on the show button. JSFiddle
Down below is my HTML table.
<table border="1" cellpadding="5" cellspacing="0" style="width: 100%">
<tr>
<td>City 1:</td>
<td>
<input id="city1"></input>
<button id="myButton1">Show</button>
<button id="clear1">Clear</button>
</td>
</tr>
<tr>
<td>City 2:</td>
<td>
<input id="city2"></input>
<button id="myButton2">Show</button>
<button id="clear2">Clear</button>
</td>
</tr>
</table>
I am using bootstrap-typehead these are as follows what's within the <head>.
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/3.1.0/bootstrap3-typeahead.js"></script>
Then there is my jQuery function, utilised to display a list of cities when the 'show' button is clicked and when 'clear' is clicked: destroy the previously initialised typeahead. This works only when I double click the show button. I got to make this work in with a singular button click.
$('#myButton1').click(function() {
$('#city1').trigger('keyup');
$('#city1').focus();
$('#city1').typeahead({
source : [ 'Alabama', 'Alaska','Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' ],
autoSelect : false,
items : 1000,
minLength : 0
});
});
$('#clear1').click(function() {
$('#city1').val('');
$('#city1').typeahead('destroy');
});
//trigger the typeahead
$('#myButton2').click(function() {
$('#city2').trigger('keyup');
$('#city2').focus();
$('#city2').typeahead({
source : [ 'Alabama', 'Alaska','Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' ],
autoSelect : false,
items : 1000,
minLength : 0
});
});
$('#clear2').click(function() {
$('#city2').val('');
$('#city2').typeahead('destroy');
});