Skip to content Skip to sidebar Skip to footer

Modify New Elements When They Are Added

This is done by my userscript on page load: $('a.people').each(function (index, value) { $(''+$(value).text()+'').insertBefore(value); }); The web app the

Solution 1:

If you want to act on new elements when the target page adds them, you have two choices:

(Note that the older Mutation events are deprecated and not the same as mutation observers.)

In addition, you'll need to track which nodes have been done and avoid repeatedly modifying them. There is a utility for this: waitForKeyElements. It uses polling and handles all the overhead. You will not notice a page slowdown.

A complete userscript, suitable for Greasemonkey or Tampermonkey, that replaces your question code, using waitForKeyElements would be:

// ==UserScript==// @name     _Italicize People entries// @include  http://YOUR_SERVER.COM/YOUR_PATH/*// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js// @grant    GM_addStyle// ==/UserScript==/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

waitForKeyElements ("a.people", italicizePeople);

functionitalicizePeople (jNode) {
    $('<i>' + jNode.text() + '</i>').insertBefore (jNode);
}

Solution 2:

you need to use jquery on() for dynamicly added elements

Post a Comment for "Modify New Elements When They Are Added"