Monday 14 March 2011

Thursday 10 March 2011

Re-applying jQuery functions in replaced/inserted content

I had an issue where a jQuery function was not working in replaced content (this was being replaced on a timer every 5 seconds).
The content had an link which had a rel attribute, we used this as a mechanism to apply the boxy function to links in the server code, and then we had a javascript file that applied the boxy function to all a tags that had a rel attribute of boxy to all the documents.

When this was running, the boxy popup didn’t work on the page (in the replaced content).

What you have to do is re-apply the jQuery again (see code). When this was done, the boxy pop up worked again. Note: after this, I've read about the live() function [http://api.jquery.com/live/] but not too sure how this works with plugin functions - haven't had the time to experiment with it yet.

 $(function () {  
   $('#div_id').updateStatus();  
 });  
 $.fn.updateStatus = function () {  
   return this.each(function () {  
     window.setInterval  
     (  
       refreshStatus,  
       5000  
     );  
     function refreshStatus() {  
       var param1 = $('#statusId').val();  
       var param2 = $('#reportsId).val();  
       $.get  
       (  
         AJAXUrls.getStatusURL, { Param1: param1 },  
         function (data) {  
           $('#status').empty().append(data);  
         }  
       );  
       $.get  
       (  
         AJAXUrls.getStatusReportURL, { Param1: param1, Param2: param2 },  
         function (data) {  
           $('#reports').empty().append(data);  
           $('a[rel=boxy]').boxy(); // Boxy re-applied.  
         }  
       );  
     };  
   });  
 }  

Tuesday 8 March 2011

Selecting text in Visual Studio

Pressing CTRL + W repeatedly selects the word, then the line, then the block of text. It’s the fastest way to select text.