EzDevInfo.com

dom interview questions

Top dom frequently asked interview questions

How to find event listeners on a DOM node when debugging or from the JS code?

I have a page where some event listeners are attached to input boxes and select boxes. Is there a way to find out which event listeners are observing a particular DOM node and for what event?

Events are attached using:

  1. Prototype's Event.observe;
  2. DOM's addEventListener;
  3. As element attribute element.onclick.

Source: (StackOverflow)

Retrieve the position (X,Y) of an HTML element

I want to know how to get the X and Y position of HTML elements such as img and div in JavaScript.


Source: (StackOverflow)

Advertisements

How to remove all CSS classes using jQuery?

Instead of individually calling $("#item").removeClass() for every single class an element might have, is there a single function which can be called which removes all CSS classes from the given element?

Both jQuery and raw JavaScript will work.


Source: (StackOverflow)

How do I select text nodes with jQuery?

I would like to get all descendant text nodes of an element, as a jQuery collection. What is the best way to do that?


Source: (StackOverflow)

JavaScript get input text value

I am working on kinda a search with JavaScript, I would use a form but it messes up something else on my page. I have this input text field:

<input name="searchTxt" type="text" maxlength="512" id="searchTxt" class="searchField"/>

and this is my JavaScript:

<script type="text/javascript">
function searchURL(){
    window.location = "http://www.myurl.com/search/" + (input text value);
}
</script>

How do I get the value from the text field into JavaScript?


Source: (StackOverflow)

jQuery find events handlers registered with an object

I need to find which event handlers are registered over an object.

For example:

$("#el").click(function() {...});
$("#el").mouseover(function() {...});

$("#el") has click and mouseover registered.

Is there a function to find out that, and possibly iterate over the event handlers?

If it is not possible on a jQuery object through proper methods, is it possible on a plain DOM object?


Source: (StackOverflow)

Checking if an element is hidden

In jQuery, it is possible to toggle the visibility of an element, using the functions .hide(), .show() or .toggle().

Using jQuery, how would you test if an element is visible or hidden?


Source: (StackOverflow)

Change an element's class with JavaScript

How can I change a class of an HTML element in response to an onClick event using JavaScript?


Source: (StackOverflow)

jQuery document.createElement equivalent?

I'm refactoring some old JavaScript code and there's a lot of DOM manipulation going on.

var d = document;
var odv = d.createElement("div");
odv.style.display = "none";
this.OuterDiv = odv;

var t = d.createElement("table");
t.cellSpacing = 0;
t.className = "text";
odv.appendChild(t);

I would like to know if there is a better way to do this using jQuery. I've been experimenting with:

var odv = $.create("div");
$.append(odv);
// And many more

But I'm not sure if this is any better.


Source: (StackOverflow)

.prop() vs .attr()

So jQuery 1.6 has the new function prop().

$(selector).click(function(){
    //instead of:
    this.getAttribute('style');
    //do i use:
    $(this).prop('style');
    //or:
    $(this).attr('style');
})

or in this case do they do the same thing?

And if I do have to switch to using prop(), all the old attr() calls will break if i switch to 1.6?

UPDATE

See this fiddle: http://jsfiddle.net/maniator/JpUF2/

The console logs the getAttribute as a string, and the attr as a string, but the prop as a CSSStyleDeclaration, Why? And how does that affect my coding in the future?


Source: (StackOverflow)

Adding options to a