Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript HTML DOM CSS

JS HTML DOM CSS

The HTML DOM allows JavaScript to change the style of HTML elements.

Changing HTML Style

To change the style of an HTML element, use this syntax:

					 
        
          element.style.property = new style
        
      

The following example changes the style of a <p> element:

					 
        
          <html>
            <body>
              <p id="examplePara">Hello World!</p>
              <script>
                document.getElementById("examplePara").style.color = "blue";
              </script>
            </body>
          </html>
        
      
Using Events

The HTML DOM allows you to execute code when an event occurs.

Events are generated by the browser when "things happen" to HTML elements:

  • An element is clicked on
  • The page has loaded
  • Input fields are changed

This example changes the style of the HTML element with id="examplePara", when the user clicks a button:

					 
        
          <html>
            <body>
              <p id="examplePara">Hello World!</p>
              <button type="button"
                    onclick="document.getElementById('examplePara').style.color = 'red'">
                    Click Me!
              </button>
            </body>
          </html>
        
      
Events Example
					 
        
          <html>
            <body>
              <p id="examplePara">This paragraph will change.</p>
              <button type="button"
                    onclick="document.getElementById('examplePara').style.color = 'red'">
                    Red Text
              </button>
              <button type="button"
                    onclick="document.getElementById('examplePara').style.color = 'black'">
                    Black Text
              </button>
              <button type="button"
                    onclick="document.getElementById('examplePara').style.display = 'block'">
                    Show Text
              </button>
              <button type="button"
                    onclick="document.getElementById('examplePara').style.display = 'none'">
                    Hide Text
              </button>
            </body>
          </html>
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved