49
<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.

1
  • Before you use it, read this "bug report" and JQ team response: bugs.jquery.com/ticket/9955 It may come handy to know. Commented Oct 23, 2011 at 16:57

7 Answers 7

34

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

Sign up to request clarification or add additional context in comments.

3 Comments

Writing a custom jQuery CSS selector provides the most control
The ":contains(...)" syntax in that URL worked for me. But the "[innerHTML=...]" produced no effect for me.
It might be a good link but does not help here :-)
18

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/

3 Comments

+1 for not thinking jQuery is the be all and end all of JavaScript.
Is this a misleading answer? I spent an hour before I get into that there is no getElements method. I needed to use document.getElementsByTagName instead.
@FLICKER answer updated with actual working code.
12

Using CSS you can't detect the content of the anchor tag.

[value=] would refer to an attribute on the tag

<a href="" value="blah"> innerHTML2 </a>

Not very useful since the value attribute isn't valid HTML on an a tag

If possible, slap a class on that a tag. As that is most likely not possible (because you would've already done that) you can use jQuery to add a class on that tag. Try something like this:

   <script type="text/javascript">
        $(function(){ $('a:contains(innerHTML2)').addClass('anchortwo'); });
    </script>

And then use .anchortwo as your class selector.

2 Comments

edited because of the random downvote with no explanation
This is the closest answer as to why! TO NOTE: css selectors allow detection of attributes, these are predefined in the HTML code, where as innerHTML is a property of the DOM layer. They may resemble access in js, but are two different things, a.href is an attribute over which CSS has knowledge, a.innerHTML is a DOM property, obscure to CSS.
0

you can use the css nth-child property to access any element and do any changes. i Used it on a website i made to make a logo smaller or bigger based on the width of screen.

3 Comments

Can you provide a simple example of what the css code would look like to help others that may not be familiar with the property or format?
for example if there is a div with class 'mark' and there are 4 <p> elements inside it, if i want to apply property to all even or odd elements, or even a single element, i can do this by n-th child property, go read about it on w3schools, as they have far better examples with all its variations, much better than what i would give here, i'm pretty bad at giving examples. ;) w3schools.com/cssref/sel_nth-child.asp
Welcome to SO! Looks like this is one of your first answers. Your method is certainly a solution, but generally answers need two things: 1. a new answer to this specific question, which might be "you can't but here's a work around" and a sample of the code to solve the problem, because links go stale/break/move. When you get a chance, check out the tour if you haven't already.
0

Using pup, a command line tool for processing HTML using CSS selectors, you can use a:contains("innerHTML1").

For example:

$ echo '<a href="example1.com"> innerHTML1 </a>' | pup 'a:contains("innerHTML1")' text{}
 innerHTML1 

Comments

-3
<style>
a[data-content]::before {
  content: attr(data-content);
}
a[data-content="innerHTML2"] {
  color: green;
}
</style>
<a href="example1.com" data-content="innerHTML1">&nbsp;</a>
<a href="example2.com" data-content="innerHTML2">&nbsp;</a>
<a href="example3.com" data-content="innerHTML3">&nbsp;</a>

2 Comments

You're not selecting the element by innerHTML. You're selecting by an attribute which has the value "innerHTML1". So, you haven't answered the question.
by the way @joe-tom, It helps me solve my problem. If something is not possible, ratoupoilu has given an alternate. This is the problem with most newly-enriched-in-reputation stackoverflowians.
-11

This is quite simple with a nth-child selector.

<style>
a:nth-child(2) {
  color: green;
}
</style>
<a href="example1.com"> innerHTML1 </a>
<a href="example2.com"> innerHTML2 </a>
<a href="example3.com"> innerHTML3 </a>

Edit: Here's the source I found this at. Check here for browser compatability. Source: http://reference.sitepoint.com/css/pseudoclass-nthchild

3 Comments

This isn't supported cross browser. IE for example won't support this
Also it isn't selecting based on inner html it is selecting based on position so it doesn't really answer the question
As said before, this does not select the element based on the content, but it selects the element based on the index which is not wat the OP asked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.