Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sharon Rosner http://hiperu.blogspot.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Easily get a single element with jQuery

While jQuery is a great help, a lot of times I just need to grab a single element. It's a real PITA to have to write $('#mydiv').get(0) all the time, so here's a solution:

   1  
   2  $.first = function(a, b) {return $(a, b).get(0)};

Get element attributes by namespace

This function returns a hash containing an elements attributes for the specified namespace. If a null namespace is specified, the function returns all attributes without a namespace.

It's optimized for speed rather than beauty.

   1  
   2  function elementAttributesNS(e, ns) {
   3    if (!this.__namespaceRegexps)
   4      this.__namespaceRegexps = {};
   5    var regexp = this.__namespaceRegexps[ns];
   6    if (!regexp) {
   7      this.__namespaceRegexps[ns] = regexp = 
   8      ns ? eval("/^" + ns + ":(.+)/") : /^([^:]*)$/;
   9    }
  10    var result = {};
  11    var atts = e.attributes;
  12    var l = atts.length;
  13    for (var i = 0; i < l; i++) {
  14      var m = atts[i].name.match(regexp);
  15      if (m)
  16        result[m[1]] = atts[i].value;
  17    }
  18    return result;
  19  }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS