Hide All But First Matching Element

Posted by Batfan on Stack Overflow See other posts from Stack Overflow or by Batfan
Published on 2010-06-14T21:25:03Z Indexed on 2010/06/14 21:42 UTC
Read the original article Hit count: 220

Filed under:
|

I am using jquery to sort through multiple paragraphs. Currently I have it set to show only paragraphs that start with a current letter. But now, I would like to consolidate further. If the text between the paragraph tags has multiple instances, I would like all but the first hidden.

This is what I have so far but, it is not working.

var letter = '<?php  echo(strlen($_GET['letter']) == 1) ? $_GET['letter'] : ''; ?>'

function finish(){
    jQuery('p').each(function(){     
    if(jQuery(this).text().substr(0,1).toUpperCase() == letter){
         jQuery(this).addClass('current-series');
         jQuery(this).html(letter + '<span class="hidden">'+jQuery(this).text().slice(1)+ '</span>');
    }
    else{ jQuery(this).hide();}
   })
}

Update:

Sorry guys, I know this is kind of hard to explain. Here's a basic example:

  • The selected letter is B
  • The values returned are:

Ball

Ball

Ball

Boy

Brain

Bat

Bat

Each of these values is in a paragraph tag. Is there a way to consolidate to this?

Ball

Boy

Brain

Bat

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about html