Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to CSS: select element based on inner HTML [duplicate]

<a href="example1.com"> innerHTML1 </a> <a href="example2.com"> innerHTML2 </a> <a href="example3.com"> innerHTML3 </a> 

I want to style the second only (innerHTML2) using CSS selectors, based on the inner HTML. Is this possible? I've tried using a[value=innerHTML2] but it doesn't seem to work.

like image 656
whamsicore Avatar asked Jan 27 '11 01:01

whamsicore


People also ask

Can you set inner HTML in CSS?

This is not possible using CSS.

How do I target a second sibling in CSS?

You use the general sibling selector (~) in combination with :hover . The ~ combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent.

How do you use text property in CSS selector?

The inner texts are the string patterns that the HTML tag manifests on the web page. ':' is used to symbolize contains method. Click on the "Find target in page" button to check whether the defined CSS Selector locates the desired element or not.

How write CSS selector in selenium using contains?

Type “css=input[type='submit']” (locator value) in Selenium IDE. Click on the Find Button. The “Sign in” button will be highlighted, verifying the locator value. Attribute: Used to create the CSS Selector.


2 Answers

This is not possible using CSS. You can, however, do it using jQuery. There's a nice blog post on it you can read.

like image 179
Marc W Avatar answered Oct 05 '22 18:10

Marc W


It's currently not possible for all browsers with css, but with javascript you can do this

Updated w/ working code. JSFiddle link below:

Initial HTML per @whamsicore:

<a href="example1.com"> innerHTML1 </a> <a href="example2.com"> innerHTML2 </a> <a href="example3.com"> innerHTML3 </a> 

JavaScript:

var myEles = document.getElementsByTagName('a'); for(var i=0; i<myEles.length; i++){     if(myEles[i].innerHTML == ' innerHTML2 '){          console.log('gotcha');            //use javascript to style          myEles[i].setAttribute('class', "gotcha");     } } 

CSS for styling:

/* make this look a bit more visible */ a{   display: block; }  .gotcha{   color: red; } 

https://jsfiddle.net/kjy112/81qqxj23/

like image 44
KJYe.Name Avatar answered Oct 05 '22 17:10

KJYe.Name