How to change the html elements by JavaScript?

Asked 19-Jul-2021
Viewed 296 times

1 Answer


1

in javascript has some methods to to change the internal data, style and attributes.

  1. element.innerHTML
  2. element.style.style-name
  3. element.setAttribute
  4. element.attribute
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
    <style>
        .page {
            background-color:green;
            }
    </style>
</head>
<body>
    <p id='p2'>Some data is available</p>
    <p id='p3'>Some data is available</p>
    <p id='p1'>Some data is available</p>
    <script>
        document.getElementById('p2').innerHTML += ' this is inner HTML';
        document.getElementById('p3').setAttribute('class', 'page');
        document.getElementById('p1').style.backgroundColor='red';
    </script>
</body>
</html>