jQuery document.createElement equivalent?

Here’s your example in the “one” line. this.$OuterDiv = $(‘<div></div>’) .hide() .append($(‘<table></table>’) .attr({ cellSpacing : 0 }) .addClass(“text”) ) ; Update: I thought I’d update this post since it still gets quite a bit of traffic. In the comments below there’s some discussion about $(“<div>”) vs $(“<div></div>”) vs $(document.createElement(‘div’)) as a way of creating new … Read more

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

The correct approach is to use element.getBoundingClientRect(): var rect = element.getBoundingClientRect(); console.log(rect.top, rect.right, rect.bottom, rect.left); Internet Explorer has supported this since as long as you are likely to care about and it was finally standardized in CSSOM Views. All other browsers adopted it a long time ago. Some browsers also return height and width properties, though … Read more

.prop() vs .attr()

Update 1 November 2012 My original answer applies specifically to jQuery 1.6. My advice remains the same but jQuery 1.6.1 changed things slightly: in the face of the predicted pile of broken websites, the jQuery team reverted attr() to something close to (but not exactly the same as) its old behaviour for Boolean attributes. John … Read more

How can I change an element’s class with JavaScript?

Modern HTML5 Techniques for changing classes Modern browsers have added classList which provides methods to make it easier to manipulate classes without needing a library: document.getElementById(“MyElement”).classList.add(‘MyClass’); document.getElementById(“MyElement”).classList.remove(‘MyClass’); if ( document.getElementById(“MyElement”).classList.contains(‘MyClass’) ) document.getElementById(“MyElement”).classList.toggle(‘MyClass’); Unfortunately, these do not work in Internet Explorer prior to v10, though there is a shim to add support for it to IE8 … Read more