How to bind the event in html template?












0















Please refer to my previous question: How to get element in template html?.



Firstly, I follow the solution in How to get element in template html? to obtain the element.



var template = templateContent.content;


Then I try to bind a click event for searchBtn:



searchBtn.addEventListener('click', function() {
console.log('click'); // does not work
});


This binding fails, so I use the event delegation to handle event:



document.addEventListener('click',function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click');
console.log(template.querySelector('#results'));
template.querySelector('#results').innerText = 'Foo';
}
});


The two logs are expected, but the last command to update the innerText does not work. On the other hand, I can update its content outside the click event. Any solution?



The final version of my code:



var link = document.querySelector('link[rel="import"][href="search.html"]');
var templateContent = link.import.querySelector('template');
var template = templateContent.content;
var searchBtn = template.querySelector('#searchBtn');
console.log(searchBtn);
template.querySelector('#results').innerText = 'Bar'; // ok

document.addEventListener('click',function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click'); // ok
console.log(template.querySelector('#results')); // ok
template.querySelector('#results').innerText = 'Foo'; // does not work
}
});


Updates



Now I retrieve the results element outside the function:



var results = template.querySelector('#results');


Then inside the function, results.innerText = 'Foo'; works. However, this only updates the results itself, and the UI is not updated correspondingly.



How to update the UI inside the template?










share|improve this question

























  • What do you mean by 'However, this only updates the results itself, and the UI is not updated correspondingly' ? what is exactly not working here? Have you tried with binding this here as described in answer?

    – Narendra Mongiya
    Nov 13 '18 at 6:04











  • this might be useful quirksmode.org/js/events_advanced.html

    – Narendra Mongiya
    Nov 13 '18 at 6:08











  • @NarendraMongiya I log the results.innerText inside the click function, and it is updated. But the nothing happens in the UI.

    – chenzhongpu
    Nov 13 '18 at 6:12











  • yes, because that is inside the scope of function.

    – Narendra Mongiya
    Nov 13 '18 at 6:13











  • but template does not have any reference inside eventlisterner without binding. The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

    – Narendra Mongiya
    Nov 13 '18 at 6:16
















0















Please refer to my previous question: How to get element in template html?.



Firstly, I follow the solution in How to get element in template html? to obtain the element.



var template = templateContent.content;


Then I try to bind a click event for searchBtn:



searchBtn.addEventListener('click', function() {
console.log('click'); // does not work
});


This binding fails, so I use the event delegation to handle event:



document.addEventListener('click',function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click');
console.log(template.querySelector('#results'));
template.querySelector('#results').innerText = 'Foo';
}
});


The two logs are expected, but the last command to update the innerText does not work. On the other hand, I can update its content outside the click event. Any solution?



The final version of my code:



var link = document.querySelector('link[rel="import"][href="search.html"]');
var templateContent = link.import.querySelector('template');
var template = templateContent.content;
var searchBtn = template.querySelector('#searchBtn');
console.log(searchBtn);
template.querySelector('#results').innerText = 'Bar'; // ok

document.addEventListener('click',function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click'); // ok
console.log(template.querySelector('#results')); // ok
template.querySelector('#results').innerText = 'Foo'; // does not work
}
});


Updates



Now I retrieve the results element outside the function:



var results = template.querySelector('#results');


Then inside the function, results.innerText = 'Foo'; works. However, this only updates the results itself, and the UI is not updated correspondingly.



How to update the UI inside the template?










share|improve this question

























  • What do you mean by 'However, this only updates the results itself, and the UI is not updated correspondingly' ? what is exactly not working here? Have you tried with binding this here as described in answer?

    – Narendra Mongiya
    Nov 13 '18 at 6:04











  • this might be useful quirksmode.org/js/events_advanced.html

    – Narendra Mongiya
    Nov 13 '18 at 6:08











  • @NarendraMongiya I log the results.innerText inside the click function, and it is updated. But the nothing happens in the UI.

    – chenzhongpu
    Nov 13 '18 at 6:12











  • yes, because that is inside the scope of function.

    – Narendra Mongiya
    Nov 13 '18 at 6:13











  • but template does not have any reference inside eventlisterner without binding. The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

    – Narendra Mongiya
    Nov 13 '18 at 6:16














0












0








0








Please refer to my previous question: How to get element in template html?.



Firstly, I follow the solution in How to get element in template html? to obtain the element.



var template = templateContent.content;


Then I try to bind a click event for searchBtn:



searchBtn.addEventListener('click', function() {
console.log('click'); // does not work
});


This binding fails, so I use the event delegation to handle event:



document.addEventListener('click',function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click');
console.log(template.querySelector('#results'));
template.querySelector('#results').innerText = 'Foo';
}
});


The two logs are expected, but the last command to update the innerText does not work. On the other hand, I can update its content outside the click event. Any solution?



The final version of my code:



var link = document.querySelector('link[rel="import"][href="search.html"]');
var templateContent = link.import.querySelector('template');
var template = templateContent.content;
var searchBtn = template.querySelector('#searchBtn');
console.log(searchBtn);
template.querySelector('#results').innerText = 'Bar'; // ok

document.addEventListener('click',function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click'); // ok
console.log(template.querySelector('#results')); // ok
template.querySelector('#results').innerText = 'Foo'; // does not work
}
});


Updates



Now I retrieve the results element outside the function:



var results = template.querySelector('#results');


Then inside the function, results.innerText = 'Foo'; works. However, this only updates the results itself, and the UI is not updated correspondingly.



How to update the UI inside the template?










share|improve this question
















Please refer to my previous question: How to get element in template html?.



Firstly, I follow the solution in How to get element in template html? to obtain the element.



var template = templateContent.content;


Then I try to bind a click event for searchBtn:



searchBtn.addEventListener('click', function() {
console.log('click'); // does not work
});


This binding fails, so I use the event delegation to handle event:



document.addEventListener('click',function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click');
console.log(template.querySelector('#results'));
template.querySelector('#results').innerText = 'Foo';
}
});


The two logs are expected, but the last command to update the innerText does not work. On the other hand, I can update its content outside the click event. Any solution?



The final version of my code:



var link = document.querySelector('link[rel="import"][href="search.html"]');
var templateContent = link.import.querySelector('template');
var template = templateContent.content;
var searchBtn = template.querySelector('#searchBtn');
console.log(searchBtn);
template.querySelector('#results').innerText = 'Bar'; // ok

document.addEventListener('click',function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click'); // ok
console.log(template.querySelector('#results')); // ok
template.querySelector('#results').innerText = 'Foo'; // does not work
}
});


Updates



Now I retrieve the results element outside the function:



var results = template.querySelector('#results');


Then inside the function, results.innerText = 'Foo'; works. However, this only updates the results itself, and the UI is not updated correspondingly.



How to update the UI inside the template?







javascript html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:58







chenzhongpu

















asked Nov 13 '18 at 3:48









chenzhongpuchenzhongpu

2,25732451




2,25732451













  • What do you mean by 'However, this only updates the results itself, and the UI is not updated correspondingly' ? what is exactly not working here? Have you tried with binding this here as described in answer?

    – Narendra Mongiya
    Nov 13 '18 at 6:04











  • this might be useful quirksmode.org/js/events_advanced.html

    – Narendra Mongiya
    Nov 13 '18 at 6:08











  • @NarendraMongiya I log the results.innerText inside the click function, and it is updated. But the nothing happens in the UI.

    – chenzhongpu
    Nov 13 '18 at 6:12











  • yes, because that is inside the scope of function.

    – Narendra Mongiya
    Nov 13 '18 at 6:13











  • but template does not have any reference inside eventlisterner without binding. The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

    – Narendra Mongiya
    Nov 13 '18 at 6:16



















  • What do you mean by 'However, this only updates the results itself, and the UI is not updated correspondingly' ? what is exactly not working here? Have you tried with binding this here as described in answer?

    – Narendra Mongiya
    Nov 13 '18 at 6:04











  • this might be useful quirksmode.org/js/events_advanced.html

    – Narendra Mongiya
    Nov 13 '18 at 6:08











  • @NarendraMongiya I log the results.innerText inside the click function, and it is updated. But the nothing happens in the UI.

    – chenzhongpu
    Nov 13 '18 at 6:12











  • yes, because that is inside the scope of function.

    – Narendra Mongiya
    Nov 13 '18 at 6:13











  • but template does not have any reference inside eventlisterner without binding. The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

    – Narendra Mongiya
    Nov 13 '18 at 6:16

















What do you mean by 'However, this only updates the results itself, and the UI is not updated correspondingly' ? what is exactly not working here? Have you tried with binding this here as described in answer?

– Narendra Mongiya
Nov 13 '18 at 6:04





What do you mean by 'However, this only updates the results itself, and the UI is not updated correspondingly' ? what is exactly not working here? Have you tried with binding this here as described in answer?

– Narendra Mongiya
Nov 13 '18 at 6:04













this might be useful quirksmode.org/js/events_advanced.html

– Narendra Mongiya
Nov 13 '18 at 6:08





this might be useful quirksmode.org/js/events_advanced.html

– Narendra Mongiya
Nov 13 '18 at 6:08













@NarendraMongiya I log the results.innerText inside the click function, and it is updated. But the nothing happens in the UI.

– chenzhongpu
Nov 13 '18 at 6:12





@NarendraMongiya I log the results.innerText inside the click function, and it is updated. But the nothing happens in the UI.

– chenzhongpu
Nov 13 '18 at 6:12













yes, because that is inside the scope of function.

– Narendra Mongiya
Nov 13 '18 at 6:13





yes, because that is inside the scope of function.

– Narendra Mongiya
Nov 13 '18 at 6:13













but template does not have any reference inside eventlisterner without binding. The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

– Narendra Mongiya
Nov 13 '18 at 6:16





but template does not have any reference inside eventlisterner without binding. The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

– Narendra Mongiya
Nov 13 '18 at 6:16












3 Answers
3






active

oldest

votes


















0














You got it right, it is the binding issue. template is not available inside click function. You need to bind this. There are many ways to do this e.g.



document.addEventListener('click',onClick.bind(this));


onClick = function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click'); // ok
console.log(template.querySelector('#results')); // ok
this.template.querySelector('#results').innerText = 'Foo'; // should work
}
}





share|improve this answer


























  • double functions?

    – chenzhongpu
    Nov 13 '18 at 5:35











  • that was the typo, edited the answer.

    – Narendra Mongiya
    Nov 13 '18 at 5:52











  • Please see my updated question. The results value is updated, but the UI remains unchanged.

    – chenzhongpu
    Nov 13 '18 at 5:59



















-1














There are many ways to get the HTML elements



You can get use



var sbtnElement = document.getElementById('searchBtn');


then add eventlistener to it



sbtnElement.addEventlistener('click', function() {
console.log('click'); // does not work
});


Naturally getElement by id will give you one element, and getElementsByClassName will return you an array of elements with the same classname. Same goes for getElementsByTagName.






share|improve this answer
























  • You may refer to my previous question: How to get element in template html?. I have to access the element in a template html.

    – chenzhongpu
    Nov 13 '18 at 4:01



















-2














hi i don't know what are you try to create for. but have you ever try to use template in script ? like this one



<script id="sampleTemplate" type="text/template">
// do the html here
</script>


So in that case you can bind event using that one. just a normal creating of html and binding javascript function.



You can refer to this blog. https://jonsuh.com/blog/javascript-templating-without-a-library/



I hope, i helped you thru this. :)






share|improve this answer
























  • You may refer to my previous question How to get element in template html?. Now I can access the element, but there are some problems with the event bindings.

    – chenzhongpu
    Nov 13 '18 at 4:00











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',
autoActivateHeartbeat: false,
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%2f53273505%2fhow-to-bind-the-event-in-html-template%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You got it right, it is the binding issue. template is not available inside click function. You need to bind this. There are many ways to do this e.g.



document.addEventListener('click',onClick.bind(this));


onClick = function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click'); // ok
console.log(template.querySelector('#results')); // ok
this.template.querySelector('#results').innerText = 'Foo'; // should work
}
}





share|improve this answer


























  • double functions?

    – chenzhongpu
    Nov 13 '18 at 5:35











  • that was the typo, edited the answer.

    – Narendra Mongiya
    Nov 13 '18 at 5:52











  • Please see my updated question. The results value is updated, but the UI remains unchanged.

    – chenzhongpu
    Nov 13 '18 at 5:59
















0














You got it right, it is the binding issue. template is not available inside click function. You need to bind this. There are many ways to do this e.g.



document.addEventListener('click',onClick.bind(this));


onClick = function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click'); // ok
console.log(template.querySelector('#results')); // ok
this.template.querySelector('#results').innerText = 'Foo'; // should work
}
}





share|improve this answer


























  • double functions?

    – chenzhongpu
    Nov 13 '18 at 5:35











  • that was the typo, edited the answer.

    – Narendra Mongiya
    Nov 13 '18 at 5:52











  • Please see my updated question. The results value is updated, but the UI remains unchanged.

    – chenzhongpu
    Nov 13 '18 at 5:59














0












0








0







You got it right, it is the binding issue. template is not available inside click function. You need to bind this. There are many ways to do this e.g.



document.addEventListener('click',onClick.bind(this));


onClick = function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click'); // ok
console.log(template.querySelector('#results')); // ok
this.template.querySelector('#results').innerText = 'Foo'; // should work
}
}





share|improve this answer















You got it right, it is the binding issue. template is not available inside click function. You need to bind this. There are many ways to do this e.g.



document.addEventListener('click',onClick.bind(this));


onClick = function(e){
if(e.target && e.target.id== 'searchBtn'){
console.log('click'); // ok
console.log(template.querySelector('#results')); // ok
this.template.querySelector('#results').innerText = 'Foo'; // should work
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 5:52

























answered Nov 13 '18 at 4:30









Narendra MongiyaNarendra Mongiya

1,470719




1,470719













  • double functions?

    – chenzhongpu
    Nov 13 '18 at 5:35











  • that was the typo, edited the answer.

    – Narendra Mongiya
    Nov 13 '18 at 5:52











  • Please see my updated question. The results value is updated, but the UI remains unchanged.

    – chenzhongpu
    Nov 13 '18 at 5:59



















  • double functions?

    – chenzhongpu
    Nov 13 '18 at 5:35











  • that was the typo, edited the answer.

    – Narendra Mongiya
    Nov 13 '18 at 5:52











  • Please see my updated question. The results value is updated, but the UI remains unchanged.

    – chenzhongpu
    Nov 13 '18 at 5:59

















double functions?

– chenzhongpu
Nov 13 '18 at 5:35





double functions?

– chenzhongpu
Nov 13 '18 at 5:35













that was the typo, edited the answer.

– Narendra Mongiya
Nov 13 '18 at 5:52





that was the typo, edited the answer.

– Narendra Mongiya
Nov 13 '18 at 5:52













Please see my updated question. The results value is updated, but the UI remains unchanged.

– chenzhongpu
Nov 13 '18 at 5:59





Please see my updated question. The results value is updated, but the UI remains unchanged.

– chenzhongpu
Nov 13 '18 at 5:59













-1














There are many ways to get the HTML elements



You can get use



var sbtnElement = document.getElementById('searchBtn');


then add eventlistener to it



sbtnElement.addEventlistener('click', function() {
console.log('click'); // does not work
});


Naturally getElement by id will give you one element, and getElementsByClassName will return you an array of elements with the same classname. Same goes for getElementsByTagName.






share|improve this answer
























  • You may refer to my previous question: How to get element in template html?. I have to access the element in a template html.

    – chenzhongpu
    Nov 13 '18 at 4:01
















-1














There are many ways to get the HTML elements



You can get use



var sbtnElement = document.getElementById('searchBtn');


then add eventlistener to it



sbtnElement.addEventlistener('click', function() {
console.log('click'); // does not work
});


Naturally getElement by id will give you one element, and getElementsByClassName will return you an array of elements with the same classname. Same goes for getElementsByTagName.






share|improve this answer
























  • You may refer to my previous question: How to get element in template html?. I have to access the element in a template html.

    – chenzhongpu
    Nov 13 '18 at 4:01














-1












-1








-1







There are many ways to get the HTML elements



You can get use



var sbtnElement = document.getElementById('searchBtn');


then add eventlistener to it



sbtnElement.addEventlistener('click', function() {
console.log('click'); // does not work
});


Naturally getElement by id will give you one element, and getElementsByClassName will return you an array of elements with the same classname. Same goes for getElementsByTagName.






share|improve this answer













There are many ways to get the HTML elements



You can get use



var sbtnElement = document.getElementById('searchBtn');


then add eventlistener to it



sbtnElement.addEventlistener('click', function() {
console.log('click'); // does not work
});


Naturally getElement by id will give you one element, and getElementsByClassName will return you an array of elements with the same classname. Same goes for getElementsByTagName.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 4:00









Scott LeeScott Lee

11




11













  • You may refer to my previous question: How to get element in template html?. I have to access the element in a template html.

    – chenzhongpu
    Nov 13 '18 at 4:01



















  • You may refer to my previous question: How to get element in template html?. I have to access the element in a template html.

    – chenzhongpu
    Nov 13 '18 at 4:01

















You may refer to my previous question: How to get element in template html?. I have to access the element in a template html.

– chenzhongpu
Nov 13 '18 at 4:01





You may refer to my previous question: How to get element in template html?. I have to access the element in a template html.

– chenzhongpu
Nov 13 '18 at 4:01











-2














hi i don't know what are you try to create for. but have you ever try to use template in script ? like this one



<script id="sampleTemplate" type="text/template">
// do the html here
</script>


So in that case you can bind event using that one. just a normal creating of html and binding javascript function.



You can refer to this blog. https://jonsuh.com/blog/javascript-templating-without-a-library/



I hope, i helped you thru this. :)






share|improve this answer
























  • You may refer to my previous question How to get element in template html?. Now I can access the element, but there are some problems with the event bindings.

    – chenzhongpu
    Nov 13 '18 at 4:00
















-2














hi i don't know what are you try to create for. but have you ever try to use template in script ? like this one



<script id="sampleTemplate" type="text/template">
// do the html here
</script>


So in that case you can bind event using that one. just a normal creating of html and binding javascript function.



You can refer to this blog. https://jonsuh.com/blog/javascript-templating-without-a-library/



I hope, i helped you thru this. :)






share|improve this answer
























  • You may refer to my previous question How to get element in template html?. Now I can access the element, but there are some problems with the event bindings.

    – chenzhongpu
    Nov 13 '18 at 4:00














-2












-2








-2







hi i don't know what are you try to create for. but have you ever try to use template in script ? like this one



<script id="sampleTemplate" type="text/template">
// do the html here
</script>


So in that case you can bind event using that one. just a normal creating of html and binding javascript function.



You can refer to this blog. https://jonsuh.com/blog/javascript-templating-without-a-library/



I hope, i helped you thru this. :)






share|improve this answer













hi i don't know what are you try to create for. but have you ever try to use template in script ? like this one



<script id="sampleTemplate" type="text/template">
// do the html here
</script>


So in that case you can bind event using that one. just a normal creating of html and binding javascript function.



You can refer to this blog. https://jonsuh.com/blog/javascript-templating-without-a-library/



I hope, i helped you thru this. :)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 3:56









Agent DroidAgent Droid

916




916













  • You may refer to my previous question How to get element in template html?. Now I can access the element, but there are some problems with the event bindings.

    – chenzhongpu
    Nov 13 '18 at 4:00



















  • You may refer to my previous question How to get element in template html?. Now I can access the element, but there are some problems with the event bindings.

    – chenzhongpu
    Nov 13 '18 at 4:00

















You may refer to my previous question How to get element in template html?. Now I can access the element, but there are some problems with the event bindings.

– chenzhongpu
Nov 13 '18 at 4:00





You may refer to my previous question How to get element in template html?. Now I can access the element, but there are some problems with the event bindings.

– chenzhongpu
Nov 13 '18 at 4:00


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273505%2fhow-to-bind-the-event-in-html-template%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

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values