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.

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

Change textNode value

If you have a specific node (of type #text) and want to change its value you can use the nodeValue property: node.nodeValue=”new value”; Note: innerText (and possibly textContent) will return/set both the current node and all descendent nodes text, and so may not be the behaviour you want/expect.

Is there a best practice for generating html with javascript

Options #1 and #2 are going to be your most immediate straight forward options, however, for both options, you’re going to feel the performance and maintenance impact by either building strings or creating DOM objects. Templating isn’t all that immature, and you’re seeing it popup in most of the major Javascript frameworks. Here’s an example … Read more

What is the best JavaScript code to create an img element

oImg.setAttribute(‘width’, ‘1px’); px is for CSS only. Use either: oImg.width=”1″; to set a width through HTML, or: oImg.style.width=”1px”; to set it through CSS. Note that old versions of IE don’t create a proper image with document.createElement(), and old versions of KHTML don’t create a proper DOM Node with new Image(), so if you want to … Read more

Adding and removing style attribute from div with jquery

You could do any of the following Set each style property individually: $(“#voltaic_holder”).css(“position”, “relative”); Set multiple style properties at once: $(“#voltaic_holder”).css({“position”:”relative”, “top”:”-75px”}); Remove a specific style: $(“#voltaic_holder”).css({“top”: “”}); // or $(“#voltaic_holder”).css(“top”, “”); Remove the entire style attribute: $(“#voltaic_holder”).removeAttr(“style”)

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