lunes, 14 de diciembre de 2009

getElementByClass

You can use javascript to getElementByTag or getElementById, but what about class?

Like most plastic surgeons at this time of year, we are currently doing some development work on one of our client's back-ends! This involves some nipping and tucking of various code including the need to update the currency displayed in various fields should the end-user decide to change it.

If the currency was being displayed in just one field, we could apply an id and use the innerHTML property. However, in this case there are multiple fields that all display the currency.

As such, a span element was applied with the class "currency" and the following javascript function (based on this one) was used:


function currency_switch(currency){
var allHTMLTags=document.getElementsByTagName("span");
for (i=0; i if (allHTMLTags.className=='currency') {
allHTMLTags.innerHTML = currency;
}
}
}

2 comentarios: