Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created January 2, 2023 22:07
Show Gist options
  • Select an option

  • Save prof3ssorSt3v3/8c35df8d8ea458cf09932def3fd2660a to your computer and use it in GitHub Desktop.

Select an option

Save prof3ssorSt3v3/8c35df8d8ea458cf09932def3fd2660a to your computer and use it in GitHub Desktop.
Code from video about attributes vs properties
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Attributes vs Properties</title>
<style>
* {
box-sizing: border-box;
font-size: 30px;
color-scheme: dark light;
}
</style>
</head>
<body>
<p
id="one"
title="hello"
class="fred charlie simon jennifer"
data-prop="some value"
abc="123"
>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quae obcaecati
voluptates iure beatae modi! Assumenda sed optio debitis ab dolore
deserunt quibusdam, ad distinctio labore quae possimus? Eligendi, ea enim.
</p>
<script>
const log = console.log;
const p = document.querySelector('p');
console.group('id-title');
log(p.id);
log(p.title); //p.title='new value'
log(p.getAttribute('id')); //setAttribute(nm, val)
log(p.getAttribute('title'));
console.groupEnd('id-title');
console.group('abc');
log(p.getAttribute('abc'));
log(p.abc);
console.groupEnd('abc');
console.group('class');
log(p.getAttribute('class'));
log(p.className);
log(p.classList.contains('steve'));
console.groupEnd('class');
console.group('data-prop');
log(p.getAttribute('data-prop'));
log(p.dataset.prop);
console.groupEnd('data-prop');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment