How many time is this image clicked

Hi folks,

I am trying to write a little JS here.

My logic:

Have an image with an id - then check how many times it has been clicked on - if clicked 3 times console log a message

Code:

	var ele = document.getElementById('single-wrapper');

	for( ele = 0; ele < 10; ele++){
		jQuery("#single-wrapper"+ele+"").click(function(){
			console.log("You have clicked: ", jQuery(this).attr('id'));            
		});
	}


	if(ele.length >= 2){
	 		console.log('clicked twice');
 }

This doesn’t seem to be working, can anyone point me in the right direction please?

Cheers,

John.

Using jQuery I cannot help you. I understand the principles of them enough but don’t write it as ES6 includes most of what that library offers.

An alternative approach to using jQuery would be:

const ele = document.getElementById('single-wrapper');
let x = 0;

ele.addEventListener('click', () => {
console.log(`You have clicked:`, ++x, `times`);

if (x >= 2) console.log(`a message`);

}

Formatting was a touch out by otherwise perfect:

const ele = document.getElementById('single-wrapper');
let x = 0;

ele.addEventListener('click', () => {
console.log(`You have clicked:`, ++x, `times`);

if (x >= 2) console.log(`a message`);

});

Thank you :slight_smile:

No problem. I see what it was. Before I had the function on one line then I added the if statement inside of it on an edit :slight_smile:

Not actually testing the code so I am happy it worked XD

1 Like