Click here to Skip to main content
15,908,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add an event listener to an object(an array of classses) which i populated using queryselectorall in javascript, i want it such that whenever i take my mouse over any of the classes, that particular class changes its color . Youshould also note that the classes were added using appendchild in js

What I have tried:

const selectElemAll = function(className) {
	return document.querySelectorAll(className);
};

let blogContent = selectElemAll(".blog-content-text");

function ReadMore(blogContentClass) {
	let contentRemove = [];
	let elipsis = "...";
	for(var i = 0; i < blogContentClass.length; i++){
		if (blogContentClass[i].innerText.length > 180) {
		let content= blogContentClass[i].innerText;
		let contentRemoveAdd = content.slice(180, content.length);
		 contentRemove.push(contentRemoveAdd);
		 blogContentClass[i].innerText = blogContentClass[i].innerText.replace(contentRemove[i], elipsis);
	    let readMoreBtn = document.createElement("a","HTMLLinkElement");
		 readMoreBtn.className = "read-more";
		 readMoreBtn.innerHTML = "Read More";
		 readMoreBtn.href = "#";
		 readMoreBtn.classList.add("read-more-btn");
		 blogContentClass[i].appendChild(readMoreBtn);
		}
	}
}
	ReadMore(blogContent)

	for(var i = 0; i < blogContent.length; i++){
		blogContent[i].addEventListener('mouseover', () => {
			blogContent[i].childNodes[1].style.color = "black";
		});
Posted
Updated 20-Aug-20 18:26pm

1 solution

Think, having a lookup at this would help: CSS Pseudo-classes[^]
Example:
CSS
a {
  color: green;
}

a:hover {
  color: yellow;
}

button:active {
  background-color: green;
}

button:focus {
  background-color: blue;
}

button:hover {
  background-color: red;
}


More references:
When do the :hover, :focus, and :active pseudo-classes apply?[^]
CSS Basics: Using :hover and :active Pseudo-Classes ← Alligator.io[^]
 
Share this answer
 
Comments
Tobynate 21-Aug-20 15:15pm    
I know there are ways to use CSS to do that but i just prefer using javascript because the element was created using javascript, so i would prefer the styling with javascript
Sandeep Mewara 21-Aug-20 17:19pm    
And you don't want to add the class at runtime via javascript?
Tobynate 21-Aug-20 22:02pm    
Ofcourse i would add the class at runtime with js

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900