721jQuery’s Live Clicks

Update Since jQuery 1.7 live() is marked for deprecation and should not be used any more. Use on() instead.

When content is dynamically created by jQuery it is not automatically inserted into the DOM. Therefore, if you dynamically add - for example - a link, and you want to capture a click on this link, it is necessary to use the live() function.it is necessary to use the on() function.



NG. The link is added to the container, but the click function is not called.

// $('.container > a').live('click', function(e) {  // pre jQuery 1.7
$(document).on('click', '.container > a', function(e){
    // do things
    e.preventDefault();
});

OK. The link is added to the DOM, and the click function is called.

Yeah.

Of course the same also goes for appended elements:

$("#container").append('');

// ...

// $('#container canvas').live('mousedown', function (e) {
$(document).on('click', '#container canvas', function(e){   // pre jQuery 1.7
    console.log(this.id);
});

693Changing the Cursor with Events when dragging an HTML5 canvas

With jQuery document ready:

$(function(){
    var canvas = $('#testCanvas')[0];
    canvas.addEventListener('mousedown', function(e){
        e.preventDefault();
        e.stopPropagation();
        e.target.style.cursor = 'move';
    }, false);
    canvas.addEventListener('mouseup', function(e){
        e.target.style.cursor = 'default';
    }, false);
});

And without...

$('#testCanvas')[0].addEventListener('mousedown', function(e){
    e.preventDefault();
    e.stopPropagation();
    e.target.style.cursor = 'move';
}, false);

$('#testCanvas')[0].addEventListener('mouseup', function(e) {
    e.target.style.cursor = 'default';
}, false);

Based on this: http://stackoverflow.com/questions/2659999/html5-canvas-hand-cursor-problems

680javascript:void(0) – the better a href=”#”

Using a href="javascript:void(0)" instead of a href="#" does not jump to the top. Sould have been clear, but still good to know.

677Forcing a Boolean value in Javascript aka ‘The double negative trick’

return !!someValueThatisNotBooleanButShouldBe;

I first encountered it at Mark Pilgrim'sDive Into HTML5. Anyone else remembers his classic Kant Pro Generator from the olden days?

654Find Largest Number in Javascript

Nice way and fast way of finding the largest number in an array.

var arr = [1233, 32, 442];
var largest = Math.max.apply(Math, arr);

http://ejohn.org/blog/fast-javascript-maxmin/

http://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array

652PHP Arrays to Javascript Objects via JSON

Moving PHP Associative Array (Dictionaries, whatever..) over for use in Javascript. In JS Associative Array are actually objects, so we might as well make it into one.

echo "var data =" . json_encode($somatic, JSON_FORCE_OBJECT) . ";

233Modifying Quicktags in WordPress

Open:

/wp-includes/js/quicktags.js

Change/Add/Delete tags to your likening.

195UIWebView, Changing filenames with Javascript

Calling JS from webViewDidFinishLoading, modifying paths to local. Before that all the images had to be stored into the local path.

http://iphoneincubator.com/blog/windows-views/uiwebview-dynamically-modify-html-documents

Interesting direction, but still looking for an elegant way to mix external sites (HTML) with local content (images, scripts...)

168Google Translate *New & Improved Bookmarklet

if (page == empty) go to Google Translate else Translate Current Page