title: How to use JQuery selectors in Node.js
body: <p>I'm trying to extract email info from HTML files in my hard drive.</p>
  
  <p>If I load the file in firefox and run jQuerify bookmarklet I can use successfully the following selector/function</p>
  
  <pre><code>window.jQuery("a.iEmail").each(function(el) {
    console.log(window.jQuery(this).attr('href'))
  });
  </code></pre>
  
  <p>But using this in Node.js is not working</p>
  
  <pre><code>var document = require("jsdom").jsdom(),
    script = document.createElement("script"),
    fs = require('fs');
  
  fs.readFile('file_1.html', 'utf-8', function(err, data){
    if (err) {
      throw err;
    }
  
    // This output the document
    //console.log(data)
  
    var window = document.createWindow(data);
  
    script.src = 'http://code.jquery.com/jquery-1.4.2.js';
    script.onload = function() {
      console.log(window.jQuery.fn.jquery);
      // outputs: 1.4.2
      //console.log(window.jQuery);
  
      /*
       * This line works if i load the local file in firefox and execute
       * the jQuerify bookmarlet
       */
      window.jQuery("a.iEmail").each(function(el) {
        console.log(window.jQuery(this).attr('href'))
      });
    };
    document.head.appendChild(script);
  });
  </code></pre>