Thread: Javascript loop
View Single Post

  #9 (permalink)  
Old 01-02-2008, 09:06 PM
davkell davkell is offline
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 66
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
Default

Will this do it:

Code:
function change_font(what){ 

    var start_of_id = what.substr(0, 2);    /*find out which group of ids it belongs to, for example, "mk" */

    var allSpans = document.getElementsByTagName("span");   // get all span elements
    for(i=0; i < allSpans.length; i++)    // loop through all the span elements
    {
        // identify the ones with starting with an id the same as the one that triggered the event
        if (allSpans[i].id.substr(0, 2) == start_of_id)
        {
            // reset any span elements starting with id="mk" to normal text
            allSpans[i].style.fontWeight = "normal";
            allSpans[i].style.color= "#000";
            allSpans[i].style.fontSize = "12";
        }
    }
    /*  Now all have normal text 
        Next customise the span element with id that was passed to function */
    document.getElementById(what).style.fontSize = "12"; 
    document.getElementById(what).style.fontWeight = "bold";
    document.getElementById(what).style.color = "#990000";
}
I don't know if that's the most efficient way of doing it, as you're looping through all the span elements on the page....but I think it works!!

Last edited by davkell; 01-02-2008 at 09:08 PM. Reason: Typo
Reply With Quote