how to add paragraph on top of div content

You may use prepend to add the paragraph at the top of the container: // HTML: <div><p>Lorem ipsum</p></div> $(‘div’).prepend(‘<p>Bla bla bla’); Update: Regarding your comment about how to fade in the paragraph – use fadeIn: $(“#pcontainer”).prepend($(‘<p>This paragraph was added by jQuery.</p>’).fadeIn(‘slow’)); A working demo:

Javascript addEventListener – using to create a mouseover effect?

You will need to setup a similar event to handle mouseout. Inside the mouseout event function, you can change the color back to the original color. var item = document.getElementById(“button”); item.addEventListener(“mouseover”, func, false); item.addEventListener(“mouseout”, func1, false); function func() { // not needed since item is already global, // I am assuming this is here just … Read more

Adding and Removing Event Listeners with parameters

This is invalid: arr[i].el.addEventListener(‘click’, do_something(arr[i])); The listener must be a function reference. When you invoke a function as an argument to addEventListener, the function’s return value will be considered the event handler. You cannot specify arguments at the time of listener assignment. A handler function will always be called with the event being passed as … Read more

How to escape HTML

I’m very surprised no one answered this. You can just use the browser it self to do the escaping for you. No regex is better or safer than letting the browser do what it does best, handle HTML. function escapeHTML(str){ var p = document.createElement(“p”); p.appendChild(document.createTextNode(str)); return p.innerHTML; } or a short alternative using the Option() … Read more

Are JavaScript DOM event handlers called in order of registration?

This has been changed with DOM3! While the DOM level 2 events specification did state When the event reaches the target, any event listeners registered on the EventTarget are triggered. Although all EventListeners on the EventTarget are guaranteed to be triggered by any event which is received by that EventTarget, no specification is made as … Read more

DOM tree traversal

Since at least Axel showed interest in an iterative solution, here it is: Given two trees which have identical structure, and a specified node within the first tree, locate the node in the second tree with the same position within the structure. If we have no other information about the two trees then the position … Read more

Performance: Pure CSS vs jQuery

CSS doesn’t have to be evaluated by the browser No. CSS is a language that you write your stylesheets in, which then have to be loaded, parsed and evaluated by the browser; see below. jQuery has to be evaluated by the browser Yes, because… jQuery goes through a scripting language Yes. jQuery is written in … Read more