Reputation: 3511
I have this HTML structure I can't edit, because of our CMS and as you can see it's missing a class/span making it difficult to style the active page, which is the '5'. Meanwhile, the end chevron is removed when the last pagination is active, which is the '7'.
Can someone provide a jquery solution to wrap <span></span>
around the '5' when its active and when the last pagination is active, which is the '7', so I may style it?
This is my fiddle to provide a demo: http://jsfiddle.net/evanmoore/yay9W/
Example 1:
<div class="pag">
<a class="prev" href="http://www.example.com">‹‹</a>
<a href="http://www.example.com">1</a>
<a href="http://www.example.com">2</a>
<a href="http://www.example.com">3</a>
<a href="http://www.example.com">4</a>
5
<a href="http://www.example.com">6</a>
<a href="http://www.example.com">7</a>
<a class="prev" href="http://www.example.com">››</a>
xx records / xx records per page / x pages
</div>
Example 2: Then should the last pagination be active, in this case its the 7, the html looks like this:
<div class="pag">
<a class="prev" href="http://www.example.com">‹‹</a>
<a href="http://www.example.com">1</a>
<a href="http://www.example.com">2</a>
<a href="http://www.example.com">3</a>
<a href="http://www.example.com">4</a>
<a href="http://www.example.com">5</a>
<a href="http://www.example.com">6</a>
7 xx records / xx records per page / x pages
</div>
This is my fiddle to provide a demo: http://jsfiddle.net/evanmoore/bqyYA/
Upvotes: 1
Views: 397
Reputation: 55750
This should run for most of the cases .
The above solution does not work in many cases such as the markup in the fiddle that i have pasted here
var elms = $('.pag').contents().filter(function () {
return this.nodeType === 3 && $.trim(this.nodeValue) != '';
});
elms.each(function () {
var num = Number(this.nodeValue);
if (!isNaN(num)) {
this.nodeValue = num;
$(this).wrap('<span class="lonerPage"></span>');
}
});
Upvotes: 2
Reputation: 2924
Assuming that we want to wrap any content that is a text node except for the last one (which holds your summary), this should work:
$('div.pag').contents(':not(:last-child)').filter(function() {
return this.nodeType == 3;
})
.wrap('<span></span>')
The .contents function is similar to the .children function, except that it also returns text nodes. The filter checks for a text node type. An added filter skips the final child node.
Here's the demo:
Upvotes: 3