How to find the html elements by using JavaScript?

0 votes
27 views
Ravi Vishwakarma asked 10 days ago in Web Application by Ravi Vishwakarma

    How to find the html elements by using JavaScript?

1 Answer

0 votes
Ethan Karla answered 7 days ago by Ethan Karla
In JavaScript has basically four method for finding the html elements.
1. document.getElementById(id-name)
2. document.getElementsByClassName(class-name)
3. document.getElementsByTagName(tag-name);
4. d.getElementsByName(name-attribute name);
Example
<!DOCTYPE html>

<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
</head>
<body>
    <p id='p2'></p>
    <p class='p3'></p>
    <p ></p>
    <p name='p1'></p>
    <script>
        document.getElementById('p2').innerHTML = 'this is id selector';
        var cls = document.getElementsByClassName('p3');
        cls[0].innerHTML = 'class selector';
        var tags = document.getElementsByTagName('p');
        tags[2].innerHTML = ' this is tag name selector';
        var names = document.getElementsByName('p1')[0];
        names.innerHTML = 'this is name selector';
    </script>
</body>
</html>