Can I change background color of paragraph when I click on a link in the page.
<a href="#p4">Go to P4</a> <p id="p4">I'm P4</p>
what I need write in css to get -
p { background-color: yellow; }
You can not change the attribute of an element by clicking on another element with CSS, but it can be with JavaScript. Just add <script>
tag to end of your <body>
and onclick()
to <a>
:
<body>
<a href="#p4" onclick="changeColor()">Go to P4</a>
<p id="p4">I'm P4</p>
<script>
function changeColor() {
document.getElementById("p4").style.backgroundColor = "yellow";
}
</script>
</body>