Simple click event in Node js not working

Simple click event in Node js not working
0

#1

I have a button on the first page you come to in the app (‘index.html’):

    <div class="container">
		<br />
		<div class="btn-container">
			<button type="submit" class="btn btn-test">test</button> 
		</div>
	</div>

On the same level I have a file called clickController.client.js:

 'use strict';

 (function () {

var test = document.querySelector('.btn-test');

test.addEventListener('click', function(){
    console.log('Congrats! You have reached the clickController!');
 });

})();

When I click the ‘test’ button, nothing appears on my browser’s console, but I get no errors. What am I not doing correctly?


#2

Try:

test.addEventListener('click', function(e){
  e.preventDefault();
  console.log('Congrats! You have reached the clickController!');
});

#3

I changed the data to :slight_smile:‘use strict’;

 (function () {

   var test = document.querySelector('.btn-test');

   test.addEventListener('click', function(e){
  e.preventDefault();
  console.log('Congrats! You have reached the clickController!');
  });

  }); 

Stopped and restarted Node.js server

Refreshed the page

Still nothing

It did however give me a “failed to load… favicon” error which I resolved (went back and added a rel=“shortcut icon” link to the head section of the html page). However, now I am back to absolutely nothing happening when I click the same button.


#4

Do you have your project on GitHub?


#5

yes indeed


#6

So where do you link to your javascript inside index.html?

EDIT: You will also have to set a static folder where the server can fetch files that you link to inside index.html. Also, you are not running your anonymous function inside clickController.client.js


#7

I just did an update to that file. Still, no dice.


#8

See the edit of my previous post.


#9

OK. Thank you very much. Let me go back and work on this some more.


#10

A post was split to a new topic: Need help with click event