How do I programmatically click on an element in JavaScript?

The document.createEvent documentation says that “The createEvent method is deprecated. Use event constructors instead.“ So you should use this method instead: var clickEvent = new MouseEvent(“click”, { “view”: window, “bubbles”: true, “cancelable”: false }); and fire it on an element like this: element.dispatchEvent(clickEvent); as shown here.

Change :hover CSS properties with JavaScript

Pseudo classes like :hover never refer to an element, but to any element that satisfies the conditions of the stylesheet rule. You need to edit the stylesheet rule, append a new rule, or add a new stylesheet that includes the new :hover rule. var css=”table td:hover{ background-color: #00ff00 }”; var style = document.createElement(‘style’); if (style.styleSheet) … Read more

Cross-platform, cross-browser way to play sound from Javascript? [duplicate]

You will have to include a plug-in like Real Audio or QuickTime to handle the .wav file, but this should work… //====================================================================== var soundEmbed = null; //====================================================================== function soundPlay(which) { if (!soundEmbed) { soundEmbed = document.createElement(“embed”); soundEmbed.setAttribute(“src”, “/snd/”+which+”.wav”); soundEmbed.setAttribute(“hidden”, true); soundEmbed.setAttribute(“autostart”, true); } else { document.body.removeChild(soundEmbed); soundEmbed.removed = true; soundEmbed = null; soundEmbed = document.createElement(“embed”); … Read more

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

How do I retrieve an HTML element’s actual width and height?

You should use the .offsetWidth and .offsetHeight properties. Note they belong to the element, not .style. var width = document.getElementById(‘foo’).offsetWidth; The .getBoundingClientRect() function returns the dimensions and location of the element as floating-point numbers after performing CSS transforms. > console.log(document.getElementById(‘foo’).getBoundingClientRect()) DOMRect { bottom: 177, height: 54.7, left: 278.5,​ right: 909.5, top: 122.3, width: 631, x: … Read more