Angular 5 - How to dynamically add event on a DOM element?











up vote
0
down vote

favorite












I'm try to implement a search input as the one on "Medium" website by adding a new suggestion (based on the input context for full text search) at the top of the list of the current suggestions. I'm on an angular 5 project and I use typeahead for research module.



So this is how I add new suggestion on top off typeahead list:






var nba = [{
"team": "Boston Celtics"
}, {
"team": "Dallas Mavericks"
}, {
"team": "Brooklyn Nets"
}, {
"team": "Houston Rockets"
}, {
"team": "New York Knicks"
}, {
"team": "Memphis Grizzlies"
}, {
"team": "Philadelphia 76ers"
}, {
"team": "New Orleans Hornets"
}, {
"team": "Toronto Raptors"
}, {
"team": "San Antonio Spurs"
}];
var teams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: nba
});
var search = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{
'team': 'xxxxx'
}]
});

$('#multiple-datasets .typeahead').typeahead({
highlight: true,
hint: false,
minlenght: 2
},

{
name: 'search-in-team',
display: 'team',
source: teams,
templates: {
header: '<div class="group">Teams</div>'
}
});

$('.typeahead').bind('typeahead:render', (event, suggestions, async, dataset) => {
$('.tt-dataset').prepend('<div class="tt-suggestion tt-selectable added" > <span class="strong"> Search for "' + $('.typeahead').val() + '"</span></div>');
});

$('.typeahead').bind('typeahead:select', (ev, suggestion) => {
alert(suggestion.team)
});

.group {
padding: .5rem .5rem 0rem .5rem;
font-family: $avenir_next;
color: $color-47;
border-bottom: 1px solid grey;
font-weight: bold;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<div id='multiple-datasets'>
<input type="text" id="sskSearch" class="typeahead" placeholder="Search">
</div>





I have an alert when I click on the suggestion under "Teams", But nothing happen when click on the new first element I manually added ("Search for 'xxxx').
enter image description here



Any help Please!!!










share|improve this question
























  • bro - your code not looks good - read some tutorials about angular first (and make examples) - this is honest and good advice
    – Kamil Kiełczewski
    Nov 22 at 17:15










  • If you found the Angular search suggestion on Medium then why are you using typeheadjs?
    – Sanjeet kumar
    Nov 22 at 17:21










  • Hi @Tatali and welcome in stackoverflow, you should provide us minimal sample of your code who can reproduce the bug.
    – Yanis-git
    Nov 23 at 7:22












  • @Sanjeetkumar, I'm tyring to use angular wtih typeahead to reproduce "Medium search tab" in my own application.
    – Lionel TADJOU
    Nov 23 at 12:21










  • @Yanis-git , I try to provide more codes and some explanations of my issues
    – Lionel TADJOU
    Nov 23 at 13:30















up vote
0
down vote

favorite












I'm try to implement a search input as the one on "Medium" website by adding a new suggestion (based on the input context for full text search) at the top of the list of the current suggestions. I'm on an angular 5 project and I use typeahead for research module.



So this is how I add new suggestion on top off typeahead list:






var nba = [{
"team": "Boston Celtics"
}, {
"team": "Dallas Mavericks"
}, {
"team": "Brooklyn Nets"
}, {
"team": "Houston Rockets"
}, {
"team": "New York Knicks"
}, {
"team": "Memphis Grizzlies"
}, {
"team": "Philadelphia 76ers"
}, {
"team": "New Orleans Hornets"
}, {
"team": "Toronto Raptors"
}, {
"team": "San Antonio Spurs"
}];
var teams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: nba
});
var search = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{
'team': 'xxxxx'
}]
});

$('#multiple-datasets .typeahead').typeahead({
highlight: true,
hint: false,
minlenght: 2
},

{
name: 'search-in-team',
display: 'team',
source: teams,
templates: {
header: '<div class="group">Teams</div>'
}
});

$('.typeahead').bind('typeahead:render', (event, suggestions, async, dataset) => {
$('.tt-dataset').prepend('<div class="tt-suggestion tt-selectable added" > <span class="strong"> Search for "' + $('.typeahead').val() + '"</span></div>');
});

$('.typeahead').bind('typeahead:select', (ev, suggestion) => {
alert(suggestion.team)
});

.group {
padding: .5rem .5rem 0rem .5rem;
font-family: $avenir_next;
color: $color-47;
border-bottom: 1px solid grey;
font-weight: bold;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<div id='multiple-datasets'>
<input type="text" id="sskSearch" class="typeahead" placeholder="Search">
</div>





I have an alert when I click on the suggestion under "Teams", But nothing happen when click on the new first element I manually added ("Search for 'xxxx').
enter image description here



Any help Please!!!










share|improve this question
























  • bro - your code not looks good - read some tutorials about angular first (and make examples) - this is honest and good advice
    – Kamil Kiełczewski
    Nov 22 at 17:15










  • If you found the Angular search suggestion on Medium then why are you using typeheadjs?
    – Sanjeet kumar
    Nov 22 at 17:21










  • Hi @Tatali and welcome in stackoverflow, you should provide us minimal sample of your code who can reproduce the bug.
    – Yanis-git
    Nov 23 at 7:22












  • @Sanjeetkumar, I'm tyring to use angular wtih typeahead to reproduce "Medium search tab" in my own application.
    – Lionel TADJOU
    Nov 23 at 12:21










  • @Yanis-git , I try to provide more codes and some explanations of my issues
    – Lionel TADJOU
    Nov 23 at 13:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm try to implement a search input as the one on "Medium" website by adding a new suggestion (based on the input context for full text search) at the top of the list of the current suggestions. I'm on an angular 5 project and I use typeahead for research module.



So this is how I add new suggestion on top off typeahead list:






var nba = [{
"team": "Boston Celtics"
}, {
"team": "Dallas Mavericks"
}, {
"team": "Brooklyn Nets"
}, {
"team": "Houston Rockets"
}, {
"team": "New York Knicks"
}, {
"team": "Memphis Grizzlies"
}, {
"team": "Philadelphia 76ers"
}, {
"team": "New Orleans Hornets"
}, {
"team": "Toronto Raptors"
}, {
"team": "San Antonio Spurs"
}];
var teams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: nba
});
var search = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{
'team': 'xxxxx'
}]
});

$('#multiple-datasets .typeahead').typeahead({
highlight: true,
hint: false,
minlenght: 2
},

{
name: 'search-in-team',
display: 'team',
source: teams,
templates: {
header: '<div class="group">Teams</div>'
}
});

$('.typeahead').bind('typeahead:render', (event, suggestions, async, dataset) => {
$('.tt-dataset').prepend('<div class="tt-suggestion tt-selectable added" > <span class="strong"> Search for "' + $('.typeahead').val() + '"</span></div>');
});

$('.typeahead').bind('typeahead:select', (ev, suggestion) => {
alert(suggestion.team)
});

.group {
padding: .5rem .5rem 0rem .5rem;
font-family: $avenir_next;
color: $color-47;
border-bottom: 1px solid grey;
font-weight: bold;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<div id='multiple-datasets'>
<input type="text" id="sskSearch" class="typeahead" placeholder="Search">
</div>





I have an alert when I click on the suggestion under "Teams", But nothing happen when click on the new first element I manually added ("Search for 'xxxx').
enter image description here



Any help Please!!!










share|improve this question















I'm try to implement a search input as the one on "Medium" website by adding a new suggestion (based on the input context for full text search) at the top of the list of the current suggestions. I'm on an angular 5 project and I use typeahead for research module.



So this is how I add new suggestion on top off typeahead list:






var nba = [{
"team": "Boston Celtics"
}, {
"team": "Dallas Mavericks"
}, {
"team": "Brooklyn Nets"
}, {
"team": "Houston Rockets"
}, {
"team": "New York Knicks"
}, {
"team": "Memphis Grizzlies"
}, {
"team": "Philadelphia 76ers"
}, {
"team": "New Orleans Hornets"
}, {
"team": "Toronto Raptors"
}, {
"team": "San Antonio Spurs"
}];
var teams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: nba
});
var search = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{
'team': 'xxxxx'
}]
});

$('#multiple-datasets .typeahead').typeahead({
highlight: true,
hint: false,
minlenght: 2
},

{
name: 'search-in-team',
display: 'team',
source: teams,
templates: {
header: '<div class="group">Teams</div>'
}
});

$('.typeahead').bind('typeahead:render', (event, suggestions, async, dataset) => {
$('.tt-dataset').prepend('<div class="tt-suggestion tt-selectable added" > <span class="strong"> Search for "' + $('.typeahead').val() + '"</span></div>');
});

$('.typeahead').bind('typeahead:select', (ev, suggestion) => {
alert(suggestion.team)
});

.group {
padding: .5rem .5rem 0rem .5rem;
font-family: $avenir_next;
color: $color-47;
border-bottom: 1px solid grey;
font-weight: bold;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<div id='multiple-datasets'>
<input type="text" id="sskSearch" class="typeahead" placeholder="Search">
</div>





I have an alert when I click on the suggestion under "Teams", But nothing happen when click on the new first element I manually added ("Search for 'xxxx').
enter image description here



Any help Please!!!






var nba = [{
"team": "Boston Celtics"
}, {
"team": "Dallas Mavericks"
}, {
"team": "Brooklyn Nets"
}, {
"team": "Houston Rockets"
}, {
"team": "New York Knicks"
}, {
"team": "Memphis Grizzlies"
}, {
"team": "Philadelphia 76ers"
}, {
"team": "New Orleans Hornets"
}, {
"team": "Toronto Raptors"
}, {
"team": "San Antonio Spurs"
}];
var teams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: nba
});
var search = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{
'team': 'xxxxx'
}]
});

$('#multiple-datasets .typeahead').typeahead({
highlight: true,
hint: false,
minlenght: 2
},

{
name: 'search-in-team',
display: 'team',
source: teams,
templates: {
header: '<div class="group">Teams</div>'
}
});

$('.typeahead').bind('typeahead:render', (event, suggestions, async, dataset) => {
$('.tt-dataset').prepend('<div class="tt-suggestion tt-selectable added" > <span class="strong"> Search for "' + $('.typeahead').val() + '"</span></div>');
});

$('.typeahead').bind('typeahead:select', (ev, suggestion) => {
alert(suggestion.team)
});

.group {
padding: .5rem .5rem 0rem .5rem;
font-family: $avenir_next;
color: $color-47;
border-bottom: 1px solid grey;
font-weight: bold;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<div id='multiple-datasets'>
<input type="text" id="sskSearch" class="typeahead" placeholder="Search">
</div>





var nba = [{
"team": "Boston Celtics"
}, {
"team": "Dallas Mavericks"
}, {
"team": "Brooklyn Nets"
}, {
"team": "Houston Rockets"
}, {
"team": "New York Knicks"
}, {
"team": "Memphis Grizzlies"
}, {
"team": "Philadelphia 76ers"
}, {
"team": "New Orleans Hornets"
}, {
"team": "Toronto Raptors"
}, {
"team": "San Antonio Spurs"
}];
var teams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: nba
});
var search = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{
'team': 'xxxxx'
}]
});

$('#multiple-datasets .typeahead').typeahead({
highlight: true,
hint: false,
minlenght: 2
},

{
name: 'search-in-team',
display: 'team',
source: teams,
templates: {
header: '<div class="group">Teams</div>'
}
});

$('.typeahead').bind('typeahead:render', (event, suggestions, async, dataset) => {
$('.tt-dataset').prepend('<div class="tt-suggestion tt-selectable added" > <span class="strong"> Search for "' + $('.typeahead').val() + '"</span></div>');
});

$('.typeahead').bind('typeahead:select', (ev, suggestion) => {
alert(suggestion.team)
});

.group {
padding: .5rem .5rem 0rem .5rem;
font-family: $avenir_next;
color: $color-47;
border-bottom: 1px solid grey;
font-weight: bold;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<div id='multiple-datasets'>
<input type="text" id="sskSearch" class="typeahead" placeholder="Search">
</div>






angular typeahead.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 13:26

























asked Nov 22 at 17:11









Lionel TADJOU

11




11












  • bro - your code not looks good - read some tutorials about angular first (and make examples) - this is honest and good advice
    – Kamil Kiełczewski
    Nov 22 at 17:15










  • If you found the Angular search suggestion on Medium then why are you using typeheadjs?
    – Sanjeet kumar
    Nov 22 at 17:21










  • Hi @Tatali and welcome in stackoverflow, you should provide us minimal sample of your code who can reproduce the bug.
    – Yanis-git
    Nov 23 at 7:22












  • @Sanjeetkumar, I'm tyring to use angular wtih typeahead to reproduce "Medium search tab" in my own application.
    – Lionel TADJOU
    Nov 23 at 12:21










  • @Yanis-git , I try to provide more codes and some explanations of my issues
    – Lionel TADJOU
    Nov 23 at 13:30


















  • bro - your code not looks good - read some tutorials about angular first (and make examples) - this is honest and good advice
    – Kamil Kiełczewski
    Nov 22 at 17:15










  • If you found the Angular search suggestion on Medium then why are you using typeheadjs?
    – Sanjeet kumar
    Nov 22 at 17:21










  • Hi @Tatali and welcome in stackoverflow, you should provide us minimal sample of your code who can reproduce the bug.
    – Yanis-git
    Nov 23 at 7:22












  • @Sanjeetkumar, I'm tyring to use angular wtih typeahead to reproduce "Medium search tab" in my own application.
    – Lionel TADJOU
    Nov 23 at 12:21










  • @Yanis-git , I try to provide more codes and some explanations of my issues
    – Lionel TADJOU
    Nov 23 at 13:30
















bro - your code not looks good - read some tutorials about angular first (and make examples) - this is honest and good advice
– Kamil Kiełczewski
Nov 22 at 17:15




bro - your code not looks good - read some tutorials about angular first (and make examples) - this is honest and good advice
– Kamil Kiełczewski
Nov 22 at 17:15












If you found the Angular search suggestion on Medium then why are you using typeheadjs?
– Sanjeet kumar
Nov 22 at 17:21




If you found the Angular search suggestion on Medium then why are you using typeheadjs?
– Sanjeet kumar
Nov 22 at 17:21












Hi @Tatali and welcome in stackoverflow, you should provide us minimal sample of your code who can reproduce the bug.
– Yanis-git
Nov 23 at 7:22






Hi @Tatali and welcome in stackoverflow, you should provide us minimal sample of your code who can reproduce the bug.
– Yanis-git
Nov 23 at 7:22














@Sanjeetkumar, I'm tyring to use angular wtih typeahead to reproduce "Medium search tab" in my own application.
– Lionel TADJOU
Nov 23 at 12:21




@Sanjeetkumar, I'm tyring to use angular wtih typeahead to reproduce "Medium search tab" in my own application.
– Lionel TADJOU
Nov 23 at 12:21












@Yanis-git , I try to provide more codes and some explanations of my issues
– Lionel TADJOU
Nov 23 at 13:30




@Yanis-git , I try to provide more codes and some explanations of my issues
– Lionel TADJOU
Nov 23 at 13:30

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435660%2fangular-5-how-to-dynamically-add-event-on-a-dom-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435660%2fangular-5-how-to-dynamically-add-event-on-a-dom-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo