i have some paragraphs. At first one of that should have red color and after click it should become black, and clicked one should become red. I need to change red each paragraph when it is clicked and remove the class when another one is clicked. I should do it with javascript
var p = document.querySelectorAll("p");
for( var i = 0; i < p.length; i++ ){
p[i].addEventListener("click", makeRed);
p[0].classList.add("active");
}
function makeRed(){
p[0].classList.remove("active");
if( this.classList.contains("active")){
this.classList.remove("active");
} else{
this.classList.add("active");
}
}
.active{
color: red;
}
<p>Text First</p>
<p>Text Second</p>
<p>Text Third</p>