Created
November 26, 2022 20:54
-
-
Save pococms/0f67770d95ea81c00dc8085991a9ee61 to your computer and use it in GitHub Desktop.
Failed attempt at getting paragraph color using DOM. Am able to get innerText though.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Trying to obtain <p> color using DOM</title> | |
<style> | |
p{color:red} | |
</style> | |
</head> | |
<body> | |
<article id="article-test"> | |
<h1>Attempting to read color of paragraph. See window.ready() function</h1> | |
<p>hello, world.</p> | |
</article> | |
<script> { | |
function ready(fn) { | |
if (document.readyState != 'loading') { | |
fn(); | |
} else if (document.addEventListener) { | |
document.addEventListener('DOMContentLoaded', fn, { | |
once: true | |
}); | |
} else { | |
document.attachEvent('onreadystatechange', function() { | |
if (document.readyState != 'loading') | |
fn(); | |
}); | |
} | |
} | |
window.ready(function() { | |
// Return HTMLCollection of all paragraphs in article. | |
var pc = document.getElementById("article-test").querySelectorAll("p"); | |
// Pick out the first paragraph. | |
p = pc.item(0) | |
// Obtain text: | |
console.log("paragraph text: " + p.innerText) // Succeeds | |
// Try to obtain color, which is styled red: | |
console.log("paragraph color: " + p.style.color) // Displays nothing | |
}); | |
} | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment