jQuery get the id/value of element after click function

$(“#myid li”).click(function() { alert(this.id); // id of clicked li by directly accessing DOMElement property alert($(this).attr(‘id’)); // jQuery’s .attr() method, same but more verbose alert($(this).html()); // gets innerHTML of clicked li alert($(this).text()); // gets text contents of clicked li }); If you are talking about replacing the ID with something: $(“#myid li”).click(function() { this.id = ‘newId’; … 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

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.

Capture iframe load complete event

<iframe> elements have a load event for that. How you listen to that event is up to you, but generally the best way is to: 1) create your iframe programatically It makes sure your load listener is always called by attaching it before the iframe starts loading. <script> var iframe = document.createElement(‘iframe’); iframe.onload = function() … Read more

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