Reputation: 47625
I have a page with a lot of hrefs listed in table rows. The user can hide/show table rows depending on seletions. I'd like to temporarily change the hrefs for the duplicates into plain text.
My thought is to do a
$('a').each(function() {
});
And then within each a element, using the nextUntil method to change any links into text that equal this.href. But then what do I do when the user chooses to hide/show a different selection? How do I refresh the links and hide the duplicates according the new filter?
I suppose I could just refresh the page, but that wouldn't be as sexy.
Upvotes: 0
Views: 652
Reputation: 19560
I don't fully understand your question, but see if this does what you need:
( function( global )
{
var $ = global.jQuery,
uniqueHREFs = {};
$( function()
{
var $matrixTable = $( '#matrix' ),
$matrixTableChildren = $matrixTable.children().detach();
$matrixTableChildren.find( 'a' ).each( function()
{
var $this = $( this ),
HREF = $this.attr( 'href' );
if( typeof uniqueHREFs[HREF] !== 'undefined' )
{
$this.replaceWith(
$( '<span>' ).addClass( 'duplicate-link-replacement' ).html( $this.html() )
);
}
else
{
uniqueHREFs[HREF] = true;
}
} );
$matrixTable.append( $matrixTableChildren );
} );
}( window ) );
It iterates all the links in your table and stores each HREF it hits which it has not previously seen. When it hits one it has previously seen, it replaces it with a span that has a class of duplicate-link-replacement
and the same HTML which the link previously had.
Upvotes: 2