jQuery Plugin To Return Delimited Value List Of Stack Element Attributes (Follow Up)

Posted July 25, 2007 at 1:42 PM

Tags: Javascript / DHTML

This is just a follow up to my previous post on the same topic. In my comments, I had mentioned that after I wrote my attrList() jQuery plugin, someone pointed out to me that the same thing was doable using an array and the jQuery mapping function (which maps an array to a function). Someone then contacted me, asking about it, so I thought I would post up the same demo, except this time, instead of building a value list string as I iterate through the jQuery stack elements, I am going to be building up an array. Then, once I have completely gone through the elements, I will simply join the array using the given delimiter.

The benefit here, while very small, is that we are not building a string as we go - we are building an array. Then, once we are through, we concatenate all the value in the array down into a single string. From what I have been told, doing this via an array has much less overhead than repetitive string concatenation (in general). However, I have also been told that Javascript arrays are just about the most poorly implemented arrays of any programming language (in terms of lookup speeds), so the speed thing might not be a factor. Using the join() method on the Javascript array is, however, much cleaner looking to me and it is certainly nice not to have to worry about the delimiter every time we add a new value.

 Launch code in new window » Download code as text file »

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Writing jQuery Plugin Demo</title>
  •  
  • <!-- Linked Files. -->
  • <script type="text/javascript" src="jquery.pack.js"></script>
  • <script type="text/javascript">
  •  
  • // This jQuery v1.1.3 plugin will return an delimited
  • // list of the given attribute of all elements in the
  • // current jQuery stack.
  •  
  • jQuery.fn.attrList = function( strAttribute, strDelimiter ){
  • // Create an array to store the attribute values of
  • // the jQuery stack items.
  • var arrValues = new Array();
  •  
  • // Check to see if we were given a delimiter.
  • // By default, we will use the comma.
  • strDelimiter = (strDelimiter ? strDelimiter : ",");
  •  
  • // Loop over each element in the jQuery stack and
  • // add the given attribute to the value array.
  • this.each(
  • function( intI ){
  • // Get a jQuery version of the current
  • // stack element.
  • var jNode = $( this );
  •  
  • // Add the given attribute value to our
  • // values array.
  • arrValues[ arrValues.length ] = jNode.attr(
  • strAttribute
  • );
  •  
  • }
  • );
  •  
  • // Return the value list by joining the array.
  • return(
  • arrValues.join( strDelimiter )
  • );
  • }
  •  
  •  
  • // This will return the ID list of all inputs
  • // with the name, "ID".
  •  
  • function GetIDs(){
  • return(
  • $( "input[@name='id']" ).attrList( "value", "-" )
  • );
  • }
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <form>
  •  
  • <!---
  • Get some hidden values. The order of the
  • value is important. We want to make sure
  • this is reflected in the value list.
  • --->
  • <input type="hidden" name="id" value="4" />
  • <input type="hidden" name="id" value="5" />
  • <input type="hidden" name="id" value="1" />
  • <input type="hidden" name="id" value="2" />
  • <input type="hidden" name="id" value="3" />
  •  
  • <input
  • type="button"
  • value="Alert Value List"
  • onclick="alert( GetIDs() );"
  • />
  •  
  • </form>
  •  
  • </body>
  • </html>

Clicking on the button properly alerts the value list:

4-5-1-2-3

I chose not to use the jQuery $.map() method, since the each() method does pretty much the same exact thing.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Aug 8, 2007 at 2:57 PM // reply »
1 Comments

Thanks I was looking for this special "join()" for jQuery :)


May 28, 2009 at 5:04 PM // reply »
1 Comments

how 'bout this-

var joinedValues = $.map($("#yourselector"),function(el){
return $(el).html();
}).join(",");


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »