Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to change an elements css based off of its inner text?

I’ve searched all over the internet for this answer and I’m still clueless, maybe one of you will know. Basically I’m wondering if in a .css file could you theoredically change the css of an object based off of its text

for example:

h1[innerText="some text thats in the h1"] {
   /*styles oh what wonderful styles*/
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

yeah you can use JavaScript with if and else statement as if condition satisfies. it will be executed to change inner HTML. Give me 2 mins to attach code. I can only do this by using js. Further properties can be introduce as here first the color was nothing but after that I introduced pink color as background.

x = document.getElementById('text')
y = x.innerHTML


if (y=="her") {
 x.innerHTML = "Paragraph changed!";
  document.getElementById("p2").style.backgroundColor = "pink";
}else{
  console.log('lol')
}
#p2{
width:200px;
height:100px;
padding-left: 10px;
}
<div class="p2" id="p2">
<h1 id='text'>her</h1>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading