I got following setup:
(function($) {
var text = $(".panel .panel-title a span").text();
var first_letter = text.charAt(0);
$(".panel-heading").attr('data-before', first_letter);
})(jQuery);
.panel-heading:before {
content: attr(data-before);
left: -25px;
top: 6px;
display: inline-block;
position: absolute;
height: 45px;
width: 47px;
z-index: 9999;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="panel">
<div class="panel-heading">
<p class="panel-title">
<a href="#aa1">
<span>Piece rate work</span>
</a>
</p>
</div>
</div>
<div class="panel">
<div class="panel-heading">
<p class="panel-title">
<a href="#aa1">
<span>Another Letter Here</span>
</a>
</p>
</div>
</div>
<div class="panel">
<div class="panel-heading">
<p class="panel-title">
<a href="#aa1">
<span>More Letters Here</span>
</a>
</p>
</div>
What I want to do is to find the first letter of the content in the span element, in this case, the letter P. And get this letter to the content of the .panel-heading::before pseudo element.
There are several .panel div's with the same sibling elements as the first .panel div inside. The span element in each of these is contains a different text, so a differtent first letter. So it should always get the relevant (first)letter and place it in the ::before content.
I managed with the help of @Michele De Falco to get the first letter and put it in the pseudo content. But it always get the first letter of the first .panel div and puts it everywhere.
I've updated your jQuery to use
each()so the correct parent.panel-headinggets the correctdata-beforeattribute: