Acquiring List of controls - Classic JS

Recently ran into a scenario where I need to use the low-level JavaScript parser, instead of JQuery.  During this venture, I ran across a situation where it would be easier for me to select all the HTML Elements with an ID, assign it to a variable as JSON, and then operate on it from that point.

So using my previous short-hand experience with JQuery, I started constructing a similar type of format to be used.

 *********** JQuery ***************  
 // One line and your done, pretty much  
 var cntrls = $('[id]')  
   
 // Later on, you simply access the element from the variable  
 var val = cntrls['someID'].value  
   
 *********** JavaScript ***********  
 // Not so one-liner; put in the DOM.Ready event for JS  
 var cntrlIDs = {}  
 var root = document.getElementById('rootID').querySelectorAll('*');  
 for (i = 0, n = root.length; i < n; ++i) {  
    var el = root[i];  
    if (el.id) {  
       cntrlIDs[el.id] = el;  
    }  
 }  
   
 // Later on, somewhere in the code  
 var id = cntrlIDs['id'];  
 var val = id.value;  

This is another great example of how simple JQuery has made utilizing JavaScript for Web Functionality.

Popular posts from this blog

SysInternals - BgInfo for ALL Users

JSON/AJAX Helpers

Linked Server....