I’m slowly migrating projects away from jQuery. It has served me really well in the past, and it still works, but it is no longer necessary. One of the few things that have been bothering me though was the lack of ‘live’-attaching of EventListeners. jQuery allows you to bind an onsubmit
event listener to any , even
s that haven’t been added to the DOM yet. It used to be called live()
(from version 1.3 to 1.9), but the functionality was later reintroduced with on()
. You would bind the event listener not to a `` directly, but to a container (up to document).
$(document).on('click', 'p.alert', function(e){
confirm("Did you read this carefully?"); }
);
Moving away from jQuery, we use the ‘standard’ addEventListener()
: but it has no easy way of delegating the events.
matches
Element.querySelector(".CSS.selector.here")
is probably quite well known by know, but I was …
There is no standard way of linking block elements to pages in HTML. But sometimes you do need it. The <a>-element doesn't support any block elements inside (such as <div> etc.). I solve the problem with a jQuery snippet that allows me to simply add 'js_blocklink' to a block elements class and the jQuery code deals with the rest. It does assume that the first link is the 'main link' (also needed for graceful degradation when Javascript for some reason is not available). A bonus feature is that other links may still exist, and stay working. function enableJsBlockLinks() {     $(".js_blocklink").each(function() {         $(this).click(function() {             $(this).find("a").each(function(index) {                 a = $(this).attr("href");                 if (a…
Today I'd like to share with you a snippet that simply uses the 'title' value of an input to make a suggestion. It degrades nicely, also users with Javascript turned off are able to get the hint, by hovering over.
$('input[title]') .val( function(i,value) { return value == "" ? $(this).attr("title") : value; }) .focus( function() { if ($(this).attr("title") == $(this).val()) $(this).val(""); } ); // added: don't submit the suggested values $('form').submit(function() { $('input[title]').val( function(i,value) { return value == $(this).attr("title") ? "" : value; } );
});
For optimal userfriendlyness, make the suggested input a little less dark than the user filled in ones.
BTW. It assumes you are already using the jQuery library.
Dit artikel van murblog van Maarten Brouwers (murb) is in licentie gegeven volgens een Creative Commons Naamsvermelding 3.0 Nederland licentie .