Using jQuery to test if an input has focus

jQuery 1.6+ jQuery added a :focus selector so we no longer need to add it ourselves. Just use $(“..”).is(“:focus”) jQuery 1.5 and below Edit: As times change, we find better methods for testing focus, the new favorite is this gist from Ben Alman: jQuery.expr[‘:’].focus = function( elem ) { return elem === document.activeElement && ( … Read more

How to distinguish between left and right mouse click with jQuery

As of jQuery version 1.1.3, event.which normalizes event.keyCode and event.charCode so you don’t have to worry about browser compatibility issues. Documentation on event.which event.which will give 1, 2 or 3 for left, middle and right mouse buttons respectively so: $(‘#element’).mousedown(function(event) { switch (event.which) { case 1: alert(‘Left Mouse button pressed.’); break; case 2: alert(‘Middle Mouse … Read more

jQuery.click() vs onClick

Using $(‘#myDiv’).click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent). Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target. var myEl = document.getElementById(‘myelement’); myEl.addEventListener(‘click’, … Read more

How to debug JavaScript / jQuery event bindings with Firebug or similar tools?

See How to find event listeners on a DOM node. In a nutshell, assuming at some point an event handler is attached to your element (eg): $(‘#foo’).click(function() { console.log(‘clicked!’) }); You inspect it like so: jQuery 1.3.x var clickEvents = $(‘#foo’).data(“events”).click; jQuery.each(clickEvents, function(key, value) { console.log(value) // prints “function() { console.log(‘clicked!’) }” }) jQuery 1.4.x … Read more

How to detect escape key press with pure JS or jQuery?

Note: keyCode is becoming deprecated, use key instead. function keyPress (e) { if(e.key === “Escape”) { // write your logic here. } } Code Snippet: var msg = document.getElementById(‘state-msg’); document.body.addEventListener(‘keypress’, function(e) { if (e.key == “Escape”) { msg.textContent += ‘Escape pressed:’ } }); Press ESC key <span id=”state-msg”></span> keyCode is becoming deprecated It seems keydown … Read more

How can I trigger the same function from multiple events with jQuery?

You can use .on() to bind a function to multiple events: $(‘#element’).on(‘keyup keypress blur change’, function(e) { // e.type is the type of event fired }); Or just pass the function as the parameter to normal event functions: var myFunction = function() { … } $(‘#element’) .keyup(myFunction) .keypress(myFunction) .blur(myFunction) .change(myFunction)

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)