jQuery get text with wildcard contents after an element and move it
Question
I think this might be a tricky one, but if anyone can solve it I'd be very impressed, and also very grateful.
Here's the existing markup (which I can't change, otherwise then it'd be an easy problem and I could solve it myself!):
<a title="1 topics" href="http://localhost/tags/amis/">amis</a> (1)
<a title="2 topics" href="http://localhost/tags/amis/">amis</a> (2)
<a title="1 topics" href="http://localhost/tags/amis/">amis</a> (1)
<a title="3 topics" href="http://localhost/tags/amis/">amis</a> (3)
What I want to do, is grab the brackets and the value inside ([WILDCARD VALUE HERE]), cut it, and append it inside the < a > tags wrapped in a < span >.
So this is what I'm after:
<a title="1 topics" href="http://localhost/tags/amis/">amis <span>(1)</span></a>
Again, if anyone has any ideas... Thanks!
Solution
Try this
$('a').each( function(){
var $this = $(this);
var textEl = this.nextSibling;
var spanEl = $('<span />').text(textEl.data);
$this.append( spanEl );
$(textEl).remove();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow