2

I have this code:

<cite>
 <a id="john" href="javascript: void(0)">John</a>
 <span>time</span>
 <a>DELETE</a>
</cite>
<blockquote>hello there</blockquote>

This is my CSS:

cite #john + blockquote {color:black;}

I am aware this won't work but I need something to select only the blockquote elements right after a cite that contains an anchor with the ID john.

I cannot change the HTML only the CSS. Any ideas please?

If this is impossible with CSS then a JavaScript solution would be welcome too.

Michael Benjamin
  • 265,915
  • 79
  • 461
  • 583
Cain Nuke
  • 1,865
  • 4
  • 29
  • 52
  • 1
    There is no `:has()` selector in css, yet. – Nenad Vracar Sep 26 '16 at 15:57
  • You can't select it with css selectors. You need to use JavaScript for this. – Mohammad Usman Sep 26 '16 at 15:58
  • Your title is not at all consistent with your description. – BoltClock Sep 26 '16 at 15:58
  • Possible duplicate of [CSS how to select first occuring element after another element](http://stackoverflow.com/questions/4623328/css-how-to-select-first-occuring-element-after-another-element) – Xavier Sep 26 '16 at 16:01
  • If impossible with CSS then a javascript solution would be welcome too. – Cain Nuke Sep 26 '16 at 16:02
  • CSS doesn't work this way. There is no [parent selector](http://stackoverflow.com/q/1014861/3597276) or [previous sibling selector](http://stackoverflow.com/q/1817792/3597276). You'll need JS. (Too bad you didn't also ask for an [`nth-of-class`](http://stackoverflow.com/q/5545649/3597276) selector. That would have been pretty cool :-) – Michael Benjamin Sep 26 '16 at 16:02
  • CSS only works in the forwards direction, which is why there is no previous sibling selector. You'll need to use javascript if you can't modify the html directly. – Sean Sep 26 '16 at 16:14

1 Answers1

3

You can use following jQuery selector to select the desired element.

$('cite').has('a#john').next('blockquote')

$('cite').has('a#john').next('blockquote').addClass('active');
.active {
  background: green;
}

blockquote {
  margin-right: 0;
  margin-left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<cite>
  <a id="john" href="javascript: void(0)">John</a>
  <span>time</span>
  <a>DELETE</a>
</cite>
<blockquote>hello there</blockquote>
<cite>
  <a href="javascript: void(0)">John</a>
  <span>time</span>
  <a>DELETE</a>
</cite>
<blockquote>hello there</blockquote>
<cite>
  <a id="john" href="javascript: void(0)">John</a>
  <span>time</span>
  <a>DELETE</a>
</cite>
<blockquote>hello there</blockquote>

Solution Without jQuery:

var activeClass = 'active';
var cites = document.querySelectorAll('cite');

[].forEach.call(cites, function(elem) {
 if(elem.querySelector('a#john')) {
        var blockQuote = elem.nextElementSibling;
        blockQuote.className = blockQuote.className + activeClass;
    }
});
.active {
  background: green;
}

blockquote {
  margin-right: 0;
  margin-left: 0;
}
<cite>
  <a id="john" href="javascript: void(0)">John</a>
  <span>time</span>
  <a>DELETE</a>
</cite>
<blockquote>hello there</blockquote>
<cite>
  <a href="javascript: void(0)">John</a>
  <span>time</span>
  <a>DELETE</a>
</cite>
<blockquote>hello there</blockquote>
<cite>
  <a id="john" href="javascript: void(0)">John</a>
  <span>time</span>
  <a>DELETE</a>
</cite>
<blockquote>hello there</blockquote>
Mohammad Usman
  • 30,882
  • 16
  • 80
  • 78