JavaScript
  Home arrow JavaScript arrow Page 3 - Building the Front End of a List Generator...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Building the Front End of a List Generator with JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-12-05

    Table of Contents:
  • Building the Front End of a List Generator with JavaScript
  • Developing the list generator’s front-end
  • Using JavaScript and the DOM
  • Integrating all the client-side code of the list generator

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building the Front End of a List Generator with JavaScript - Using JavaScript and the DOM


    (Page 3 of 4 )

    As I expressed in the section you just read, creating the set of additional input boxes required to provide each wrapped link with values for its “href” attribute and label respectively is only a matter of defining an appropriate JavaScript function that performs that task.

    Therefore, below I listed the definition for the brand new “displayListGenerator()” function. It will take care of creating dynamically all the input boxes that I mentioned before by using some simple JavaScript statements and the DOM.

    The signature for this function is as follows:

    // display unordered list input boxes function displayListGenerator(selObj){ // check if user has selected the 'none' option if(selObj.value=='none'){alert('Please specify the number of
    list items to be created');return}; // remove existing 'gencontainer' DIV var div=document.getElementById('gencontainer'); if(div){div.parentNode.removeChild(div)}; // remove existing 'listcontainer' DIV var div=document.getElementById('listcontainer'); if(div){div.parentNode.removeChild(div)}; // create general containing DIV var div=document.createElement('div'); div.setAttribute('id','gencontainer'); div.className='elemcontainer'; // create form for list items var form=document.createElement('form'); // generate input boxes for list items for(var i=1;i<=selObj.value;i++){ var p=document.createElement('p');                 // append label to paragraph                 p.appendChild(document.createTextNode('Link value '));                 // create input box for href attribute                 var hrefbox=document.createElement('input');                 hrefbox.setAttribute('type','text');                 hrefbox.setAttribute('title','Enter value for "href"
    attribute here');                 hrefbox.className='hrefbox';                 // append href box to paragraph                 p.appendChild(hrefbox);                 // append label to paragraph                 p.appendChild(document.createTextNode('Link label '));                 // create input box for link label                 var labelbox=document.createElement('input');                labelbox.setAttribute('type','text');                 labelbox.setAttribute('title','Enter label of link here');                 labelbox.className='labelbox';                // append label box to paragraph               p.appendChild(labelbox);                 // append paragraph to form                 form.appendChild(p);             } var p=document.createElement('p'); // create 'Generate list now!' button var button=document.createElement('input'); button.setAttribute('type','button'); button.setAttribute('value','Generate XHTML list now!'); button.className='button'; // append button to paragraph p.appendChild(button); // append paragraph to form form.appendChild(p); // append form to general DIV div.appendChild(form); // append general DIV to document tree document.getElementsByTagName('body')[0].appendChild(div); // assign 'onclick' event handler to button button.onclick=displayList; }

    If you examine the source code for the above function, then you’ll understand quickly what it does, particularly if the DOM and its methods are pretty familiar to you. As you can see, this JavaScript function first creates a new DIV, then appends the corresponding input boxes to this containing element, and finally inserts a new button into the container for triggering yet another specific function called “displayList().”

    However, at least during the course of this article, I’d like you to pay attention to the group of tasks performed by the prior function. As you’ll probably remember from the previous section, the function in question will be triggered when the user selects the number of list items to be created via the respective select box, therefore this operation can be graphically represented by the following image:

    After studying the above picture, hopefully you’ll have a more accurate idea of how the “displayListGenerator()” function does its business. Now, do you see how all the input boxes that allow users to enter the values for the respective “href” attribute and label of each list-wrapped link are dynamically created? I bet you do!

    Of course, aside from showing you the signature for the “displayListGenerator()” that you just learned, here is the short JavaScript snippet that attaches an “onchange” event handler to the initial select menu after loading the respective web document. As you saw previously, this event also fires up the mentioned function, thus the definition for the script is as follows:

    // execute script when web page is loaded window.onload=function(){ if(document.getElementById&&document.createElement&&document.
    createTextNode){ var sel=document.getElementsByTagName('form')[0].
    elements[0]; if(!sel){return}; sel.onchange=function(){displayListGenerator(this)};             } }

    Okay, at this stage you’ve learned not only how the front-end that corresponds to this list generator was created, but how the application uses the DOM for building dynamically all the additional input boxes required for constructing list-wrapped links. Pretty handy, right?

    However, there’s still an extra step that I need to take to conclude this first article of the series. I need to list the complete definition of this JavaScript-based list generator, including all the client-side code that was developed in the previous sections.

    That being said, click on the link that appears below and read the final section of the article. We’re almost done!

    More JavaScript Articles
    More By Alejandro Gervasio


       · Want to have at hand a simple JavaScript application that creates list-wrapped links...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT