Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2013, 01:23 PM   PM User | #1
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Suggestions for how to look at first letter, add letter block

Hello,

OK, at this point I have populated a glossary div. When a user clicks on a term this glossary pops-up with all of the terms taken from an XMl file in alphabetical order.

Now what I am trying to do is add the letters 'A', 'B', etc at the beginning of each block of terms beginning with that letter.

Is there an easy way to do this? I was thinking about creating an array with all of the letters of the alphabet, checking the first letter of each term, if it matches, create a new span with that letter, increment the array pointer to look for the next letter.

Is this an efficient way to do it?

This is what I have so far:
Code:
		$(glossaryXML).find('item').each(function()
		{
			if($(this).attr('term').substr(0,1) == 'A')
			{
				$("#glossaryContent").append("<br /><span class='glossAcro'>" + $(this).attr('term') + "</span><br />");
			}
			$("#glossaryContent").append("<br /><span class='glossAcro'>" + $(this).attr('term') + "</span>" + "<span class='glossDef'>" + $(this).attr('def') + "</span><br />");
			
		});
m2244 is offline   Reply With Quote
Old 02-14-2013, 01:39 PM   PM User | #2
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Well, I already see a problem.

If there isn't a term that begins with the next letter, it breaks. In other words if the code is looking for 'Q', but there aren't any terms that begin with 'Q' the array index does not increment so the code looks for 'Q' and misses all of the letters after 'Q'.


Code:
var letterArr2 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
		var letteArrIndex = 0;
		$(glossaryXML).find('item').each(function()
		{
			if($(this).attr('term').substr(0,1).toUpperCase() == letterArr2[letteArrIndex])
			{
				$("#glossaryContent").append("<br /><span class='glossAcro'>" + letterArr2[letteArrIndex] + "</span><br />");
				letteArrIndex++;
			}
			$("#glossaryContent").append("<br /><span class='glossAcro'>" + $(this).attr('term') + "</span>" + "<span class='glossDef'>" + $(this).attr('def') + "</span><br />");
			
		});
m2244 is offline   Reply With Quote
Old 02-14-2013, 08:49 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
How about this?
Code:
    var letters = "abcdefghijklmnopqrstuvwxyz";
    $(glossaryXML).find('item').each(function()
    {
        var first = ($(this).attr('term').charAt(0).toLowerCase();
        if ( letters.indexOf(first) >= 0 ) /* if that letter is still in the list of letters... */
        {   
            letters = letters.replace(first,"*"); /* kill that letter so can't be found again */
            $("#glossaryContent").append(...);
        }
        $("#glossaryContent").append(...);
    } );
Sneaky?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:43 AM.


Advertisement
Log in to turn off these ads.