Change CSS class from JavaScript

Recently, I was working on Magento Customization. I encountered a small problem. The css class which was applied to div tag by Magento was disturbing whole layout on particular page. So I need to set my own class over there.

I was having two options.

1) Change css class in Magento core file. However, this was impossible as this change will affect all other pages which was not desirable.

2) Change css class on particular page when page loads.

I choose second option. OnLoad of a body I called a JavaScript function. In JavaScript, I have used following code to change class.

document.getElementById(‘myDiv’).className=’newClassName’;

When page loads first time, it will be a disturbed layout but once body on load event gets fired the layout will be proper.

This entry was posted in Uncategorized on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

1 thought on “Change CSS class from JavaScript”

  1. Hiren

    Hi Kunal

    Thanks for your comment.

    Yes you are right we can use jquery but Magento use Prototype js. It also use same syntax as jQuery. So if we want to use jQuery with Magento we have to resolve this conflict first. To avoid this I have used pure JavaScript.

    Reply ↓

Leave a Reply

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