Blog

The Haycroft Media Blog covers all aspects of website design and development from the web browsers being used through to the standards that govern the world wide web. As a website design and development company, it is imperative that we keep abreast of all the latest news and happenings in our field, and here we present the major stories along with tutorials, tips and other web-programming related items.

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<allHTMLTags.length; i++) {
		if (allHTMLTags[i].className=='currency') {
			allHTMLTags[i].innerHTML = currency;
		}
	}
}
[ Back ]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Facebook Comments