EzDevInfo.com

javascript-events interview questions

Top javascript-events frequently asked interview questions

window.onload vs $(document).ready()

What are the differences between JavaScript's window.onload and JQuery's $(document).ready() method?


Source: (StackOverflow)

jQuery lose focus event

I'm trying to show up a container if a input field gets the focus and - that's the actual problem - hide the container if focus is lost. Is there an opposite event for jQuery's focus?

Some example code:

<input type="text" value="" name="filter" id="filter"/>

<div id="options">some cool options</div>

<script type="text/javascript">
  $('#options').hide();

  $('#filter').focus(function() {
    $('#options').appear();
  });
</script>

And what I'd like to do is something like this:

$('#filter').focus_lost(function() {
  $('#options').hide();
});

Source: (StackOverflow)

Advertisements

jQuery callback on image load (even when the image is cached)

I want to do:

  $("img").bind('load', function() {
    // do stuff
  });

But the load event doesn't fire when the image is loaded from cache. The jQuery docs suggest a plugin to fix this, but it doesn't work


Source: (StackOverflow)

Pass mouse events through absolutely-positioned element

I'm attempting to capture mouse events on an element with another absolutely-positioned element on top of it.

Right now, events on the absolutely-positioned element hit it and bubble up to its parent, but I want it to be "transparent" to these mouse events and forward them on to whatever is behind it. How should I implement this?


Source: (StackOverflow)

jQuery check if event exists on element [duplicate]

This question already has an answer here:

Is there a way to check if an event exists in jQuery? I'm working on a plugin that uses custom namespaced events, and would like to be able to check if the event is binded to an element or not.


Source: (StackOverflow)

window.onload vs document.onload

Which is more widely supported: window.onload or document.onload?


Source: (StackOverflow)

Listening for variable changes in JavaScript or jQuery

Is it possible to have an event that fires when the value of a certain variable changes? Thanks!


Source: (StackOverflow)

Enter key press event in JavaScript

I have a form with two text boxes, one select drop down and one radio button. When the enter key is pressed, I want to call a javascript function (User defined), but when I press it, the form is submitted.

How do I prevent the form from being submitted when the enter key is pressed?


Source: (StackOverflow)

How to distinguish between left and right mouse click with jQuery

How do you obtain the clicked mouse button using jQuery?

$('div').bind('click', function(){
    alert('clicked');
});

this is triggered by both right and left click, what is the way of being able to catch right mouse click? I'd be happy if something like below exists:

$('div').bind('rightclick', function(){ 
    alert('right mouse button is pressed');
});

Source: (StackOverflow)

What is event bubbling and capturing?

What is the difference between event bubbling and capturing? Of the two, which is the faster and better model to use?


Source: (StackOverflow)

On - window.location.hash - change?

I am using Ajax and hash for navigation. Is there a way to check if the window.location.hash changed like this?

http://example.com/blah#123 to http://example.com/blah#456

It works if I check it when the document loads. But if I have #hash based navigation it doesn't work when I press the back button on the browser (so I jump from blah#456 to blah#123). It shows inside the address box, but I can't catch it with JavaScript.


Source: (StackOverflow)

How to trigger event in JavaScript?

I have attached an event to a text box using addEventListener. It works fine. My problem arose when I wanted to trigger the event programmatically from another function.

How can I do it?


Source: (StackOverflow)

How to get all properties values of a Javascript Object (without knowing the keys)?

If there is an Javascript object:

var objects={...};

Suppose, it has more than 50 properties, without knowing the property names (that's without knowing the 'keys') how to get each property value in a loop?


Source: (StackOverflow)

How to detect pressing Enter on keyboard using jQuery?

I would like to detect whether the user has pressed Enter using jQuery.

How is this possible? Does it require a plugin?

EDIT: It looks like I need to use the keypress() method.

I wanted to know if anyone knows if there are browser issues with that command - like are there any browser compatibility issues I should know about?


Source: (StackOverflow)

jQuery - Fire event if CSS class changed

how could I fire a event if the a css class was added or changed with jQuery? Does changing of CSS class fire the jQuery change() event?


Source: (StackOverflow)