Conveting jQuery functions to JavaScript

Hi,

I have the following two functions that is coded in jQuery and I want to convert them to pure JavaScript if possible.

$(document).keyup(function(e) {
	if(e.keyCode === 27) {
		...
	}
});
$(document).mouseup(function (e) {
	var container = $('#box');
	if (!container.is(e.target) && container.has(e.target).length === 0) {
		...
	}
});

Any tips will be appreciated, thanks.

Not tested, but it seems like:
window.onkeyup = function(e){
if(e.keyCode === 27){

}
}
… should work.

Not sure about the second one.

Yes, that worked. Thank you very much :slight_smile:

Tried this for the other one but didn’t work:

window.onmouseup = function(e) {
	var container = document.getElementById('box');
	if (!container.is(e.target) && container.has(e.target).length === 0) {
		...
	}
}

I’m pretty sure that “.is” isn’t a JavaScript thing. I think it’s jQuery. Perhaps try “if(!container === e.target)” for the first part. Again, not sure about the second part.

Hi,

Give this site a go: http://youmightnotneedjquery.com/

It offers vanilla alternatives to jQueryisms (if that’s a word).

If it isn’t a word, “jQueryisms” gets my vote!