join array elements, add array elements to dropdown list using jQuery

by Developer 23. January 2010 18:33

To perform any action on arrays by using normal javascript is very difficult. To do simple action like joining array elements also, we have to define our own functions.

But by using jQuery we can perform many typical actions on arrays.

 

In this article I am explaining some simple actions on arrays by using jQuery. In my coming articles I will explain more and more actions.

 

<html>
    <head>
        <title>javascript arrays using jQuery function</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var elems = ['Asp.Net', 'Vb.Net', 'C#.Net', 'Asp.Net MVC', 'Sharepoint']

                //join array elements and add those elements to "joinarr" div tag
                $(joinarr).text(elems.join('-'));
                //display array elements one by one
               // $(joinarr).html(elems.join('<br/>'));

                //adding array elements to dropdownlist
                var sellist = $('#selarr');
                $.each(elems, function(val, text) {
                    sellist.append($('<option value=' + val + '>' + text + '</option>'));
                });

                //displaying the dropdownlist elements
                var lans = $('#disarr').children();
                var selitemstext;
                selitemstext =  'There are ' + lans.length + ' elements in "disarr" dropdownlist and elements are: ';
                $('#disarr').children().each(function() {
                selitemstext += $(this).text() + ', ';
                });
                $('#selitems').text(selitemstext);
            });   
        </script>
    </head>
    <body>
       <div id="joinarr"></div>
        <br />
        <select id="selarr"></select>
        <br />
         <select id="disarr"><option value="0">Asp.Net</option><option value="1">Vb.Net</option><option value="2">C#.Net</option><option value="3">Asp.Net MVC</option><option value="4">Sharepoint</option></select>
         <br />
         <p id="selitems"></p>
    </body>
</html>

 

Here we have javascript array "elems" which has some elements. We are performing different actions on it. I am explaining one by one.

 

                var elems = ['Asp.Net', 'Vb.Net', 'C#.Net', 'Asp.Net MVC', 'Sharepoint']

                   //join array elements and add those elements to "joinarr" div tag
                                   $(joinarr).text(elems.join('-'));

In the above code we are adding the all items of array "elems" to <div> tag whose id is "joinarr" as normal text. Here we used join()  function to add all array elements with seperator "-".

 

                      var elems = ['Asp.Net', 'Vb.Net', 'C#.Net', 'Asp.Net MVC', 'Sharepoint']

                   //display array elements one by one
                                   $(joinarr).html(elems.join('<br/>'));


Some times we have to display all array elements one by one. For this type of requirement we will use html() method to render text as HTML by giving <br/> as seperator to "join()" method.

 

                  var elems = ['Asp.Net', 'Vb.Net', 'C#.Net', 'Asp.Net MVC', 'Sharepoint']

                     //adding array elements to dropdownlist
                      var sellist = $('#selarr');
                      $.each(elems, function(val, text) {
                                    sellist.append($('<option value=' + val + '>' + text + '</option>'));
                        });

 

In some situations, we have to add javascript array elements to the HTML control. For that we have append()  jQuery method to append elements to HTML control. In the above code we are appending the our javascript array elements to the HTML drop down list control whose id is "selarr".

 

                //displaying the dropdownlist elements
                         var lans = $('#disarr').children();
                        var selitemstext;
                        selitemstext =  'There are ' + lans.length + ' elements in "disarr" dropdownlist and elements are: ';
                        $('#disarr').children().each(function() {
                        selitemstext += $(this).text() + ', ';
                        });
                       $('#selitems').text(selitemstext);

 

Even we can get drop down list items and we can add these items to other HTML control. In the above code we are getting the "disarr" drop down list items and we are adding all these items to <p> tag whose id is "selitems".

 

Download source code: simple_array_funs.zip (36.01 kb)

Comments

5/29/2010 9:13:50 PM #

Forrest Biddiscombe

Please, can you email me and tell me few more thinks about this, I am really fan of your blog...

Forrest Biddiscombe

5/30/2010 1:09:04 AM #

Masako Syta

Interesting article. Were did you got all the information from...

Masako Syta

6/6/2010 3:23:56 AM #

เล่นเกมส์ฟรี

good work, will back soon, great site congratulation!!

เล่นเกมส์ฟรี

6/7/2010 11:56:44 PM #

Marth Dusik

so when does homer and the boyz get a beer tour of wisconsin its ben overdo 30 drunkin days a place were hes not fat its a beer belly and proud of it.

Marth Dusik

6/10/2010 6:39:48 AM #

extra income

I don't bother trying to compile a list myself anymore. I use a service like Mammoth List that is affordable but high quality.

extra income

6/17/2010 9:59:20 AM #

vibram five fingers

I am truly pleased to post my comment on this weblog, numerous thanks to the author.

vibram five fingers

6/18/2010 10:03:35 AM #

Brigette Cartez

Here is the 2nd occasion I have come across your blog post in the last couple weeks.  Looks like I should bookmark it.

Brigette Cartez

6/26/2010 7:02:08 AM #

moshi monsters

http://wilbur97norton.xanga.com/729169723/be-free-from-debt-with-the-help-of-a-greenville-sc-bankruptcy-attorney/

moshi monsters

6/29/2010 11:15:07 PM #

abercrombie

Website really attractive. Congratulations!

abercrombie

6/30/2010 9:36:53 AM #

Best Penis Enlargement Product

Thanks for the insight on this topic. It�s been tough to locate advice it seems.

Best Penis Enlargement Product

7/8/2010 1:40:46 PM #

top ten job interview questions

Maybe you have considered putting a few yahoo clips to your blog to keep your readers more entertained? I mean I just read through the entire blog of yours and it was fine but since folks are more of a visual learner, I found that to become more helpful. Well you should let me know how it turns out! I like what you people are doing and up too. Such smart work and reading! Keep up the great work guys. I've included you guys to my blogroll. This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.

top ten job interview questions

7/9/2010 6:21:59 AM #

male enhancement

Longer sex + larger penis = happy wife. It works regarding me, anyway.

male enhancement

7/12/2010 8:51:49 AM #

engagement rings

There's nothing wrong with buying yourself a gift each now along with again.

engagement rings

7/18/2010 9:35:35 AM #

accutane lawsuits

Hopefully these acne tips will help me cure my case. It's so embarassing.

accutane lawsuits

7/23/2010 9:49:08 AM #

pozycjonowanie warszawa

Date Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject

pozycjonowanie warszawa

8/7/2010 2:56:55 AM #

lexi gomvents

i remember when i used to have acne, stuff just wouldn't go away, i have tried a ton of products and none of them actually work. but i did find one, its kinda nice, it works great. you guys should check it out.

lexi gomvents

8/9/2010 7:12:34 PM #

dave

My home we've got tons of getaway owners - So we were hit very hard by the housing down turn

dave

8/10/2010 7:17:11 PM #

jason

I hope things get better i our local area

jason

8/29/2010 12:56:25 AM #

Natural Cleaning Products for a Healthy Home

this was one of your better posts

Natural Cleaning Products for a Healthy Home

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Page List

    Tag cloud