How to make the code so if a div gets clicked


#1

I'm working on a project and I don't know how to make the code so if a div gets clicked it will change a variable from false to true. Help?


#2

I think you can do this with jQuery. Have you tried that language out yet?


#3

I know you can do it with jQuery but I don't know how to do it.


#4

Hi Edwin,

There are a couple ways you could do it, but I'm just going to choose one method for jQuery and JavaScript.
I recommend using Vanilla JS whenever possible, since that will make your pages load faster.

Using jQuery:

var someVar = false;

$('#divs-id').click(function() {
  someVar = true;
});

Using Vanilla JavaScript

var someVar = false,
    divsId = document.querySelector('#divs-id')

divsId.addEventListener('click', function() {
  someVar = true;
});