tags:

views:

87

answers:

2

Hi I need some help understanding this code: It works well but can someone add some comments to help me understand it better? Thanks

Here's the code:

function contractall() {
    if (document.getElementById) {
        var inc = 0
        while (document.getElementById("dropmsg" + inc)) {

            document.getElementById("dropmsg" + inc).style.display = "none"
            inc++
        }
    }
}



function expandone() {
    if (document.getElementById) {
        var selectedItem = document.dropmsgform.dropmsgoption.selectedIndex
        contractall()
        document.getElementById("dropmsg" + selectedItem).style.display = "block"
    }
}

if (window.addEventListener) window.addEventListener("load", expandone, false)
else if (window.attachEvent) window.attachEvent("onload", expandone)
A: 

The function contract all finds all the elements with the id of dropmsg0, dropmsg1 ... dropmsgN and hides them. The function expanddone shows the selected element. This is done by setting the style on the elements.

The last 2 lines are an incomplete attempt at browser comparability.

It would work in more browsers if a cross browser library like jQuery were used.

Philip Schlump
A: 
function contractall() {
    if (document.getElementById) {
        var inc = 0; // counter
        // loop on all dropmsg in the document
        while (document.getElementById("dropmsg" + inc)) {
            // hide one by one
            document.getElementById("dropmsg" + inc).style.display = "none"
            inc++
        }
    }
}



function expandone() {
    if (document.getElementById) {
        var selectedItem = document.dropmsgform.dropmsgoption.selectedIndex; // get the selected item
        contractall() // hide all in the document
        // show the selected item
        document.getElementById("dropmsg" + selectedItem).style.display = "block"
    }
}

// add event handlers to windows load.
if (window.addEventListener) window.addEventListener("load", expandone, false)
else if (window.attachEvent) window.attachEvent("onload", expandone)
Amr ElGarhy