Topic 14: DOM - getting elements from document(html file): ByTagName, ByCommonFormula

//Regular lectur 26: 5 Nov 2022

//Topic 14//* DOM *//getting elements from document(html file) //ByTagName,ByCommonFormula


html code:

<!DOCTYPE html>
<html>
    <head>
       <title>MyPage</title> 
    </head>
    <body>
        <h1>TCA Skill Lab1</h1>
        <h2 id = "virat" class = "batsman">Virat Kohli</h2>
        <h2 id = "rahul" name = "KL">KL Rahul</h2>
        <h2 id = "rohit" class ="batsman" name = "RS">Rohit Sharma</h2>
        <h2 id = "surya" class = "batsman">Suryakumar</h2>
        <h1>BCCI</h1>

        <script src = "script15.js"></script>
    </body>
</html>


JavaScript code:

//3) getElementByTagName

document.getElementsByTagName('h1')[0].addEventListener('click',function(){
    document.getElementsByTagName('h1')[0].textContent = "Unacademy"
})

document.getElementsByTagName('h2')[1].style.backgroundColor = 'red'

document.getElementsByTagName('h2')[1].style.color = 'white'


//4) getElementByCommonFormula

document.querySelector('h2[id = "virat"]').style.backgroundColor = 'yellow'

document.querySelector('h2[name = "RS"]').style.backgroundColor = 'yellow'

टिप्पण्या