Skip to content

Instantly share code, notes, and snippets.

@mohammadYousefiDev
Last active January 15, 2020 15:16
Show Gist options
  • Save mohammadYousefiDev/79f165a1ef3b355a206ea0304abba224 to your computer and use it in GitHub Desktop.
Save mohammadYousefiDev/79f165a1ef3b355a206ea0304abba224 to your computer and use it in GitHub Desktop.
1. access to element in javascript
<html>
<head></head>
<body>
<p name="codepenName" id="codepenID" class="codepenClass">Hello World</p>
<script>
//way1
/*
var x = window.document.getElementById('codepenID')
x.style.color = 'blue'
*/
//way2
/*
var x = window.document.getElementsByTagName('P')
x[0].style.color = 'blue'
*/
//way3
/*
var x = window.document.getElementsByClassName("codepenClass")
x[0].style.color = 'red'
*/
//way4
/*
var x = window.document.getElementsByName('codepenName')
x[0].style.color = 'green'
*/
//way5
/*
var x = window.document.querySelector('#codepenID')
x.style.color = 'green'
*/
//way6
/*
var x = window.document.querySelectorAll('p')
x[0].style.color = 'red'
*/
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment