Solved

iterating through table with jquery

Posted on 2008-10-21
4
4,973 Views
Last Modified: 2012-05-05
Hi experts!

I have a table containing a list of values and checkboxes applicable to these values (please see code snippet below).

What I need to is go through the rows in that table and check of one of the two (or both) checkboxes has been selected, if so I need to do something with that record (this bit I can handle), just need to know how to iterate through the table and check for checkboxes.

Many thanks in advance!
<table STYLE="table-layout:fixed" id="languagesList">

        <col width="200">
 

        <col width="100">

        <col width="100">

        <col width="1">		

        

                <tr>

                    <td align="left" id="serviceLanguageName" name="ROMANIAN ">ROMANIAN </td>

                    <td align="center" id="guide"><input type="checkbox" name="guide:ro:ROMANIAN " id="guide:ro" value="Y" class="check"  /></td>

                    <td align="center" id="audio"><input type="checkbox" name="audio:ro:ROMANIAN " id="audio:ro" value="Y" class="check"  /></td>

                    <td><input type="hidden" name="srvLang" id="srvLang" value="ROMANIAN "></>

                        

                </tr>
 

        

                <tr>

                    <td align="left" id="serviceLanguageName" name="CHINESE ">CHINESE </td>

                    <td align="center" id="guide"><input type="checkbox" name="guide:zh:CHINESE " id="guide:zh" value="Y" class="check"  checked /></td>

                    <td align="center" id="audio"><input type="checkbox" name="audio:zh:CHINESE " id="audio:zh" value="Y" class="check"  /></td>

                    <td><input type="hidden" name="srvLang" id="srvLang" value="CHINESE "></>

                        

                </tr>

        

                <tr>

                    <td align="left" id="serviceLanguageName" name="FILIPINO ">FILIPINO </td>
 

                    <td align="center" id="guide"><input type="checkbox" name="guide:tl:FILIPINO " id="guide:tl" value="Y" class="check"  checked /></td>

                    <td align="center" id="audio"><input type="checkbox" name="audio:tl:FILIPINO " id="audio:tl" value="Y" class="check"  checked /></td>

                    <td><input type="hidden" name="srvLang" id="srvLang" value="FILIPINO "></>

                        

                </tr>

        

                <tr>

                    <td align="left" id="serviceLanguageName" name="CATALAN ">CATALAN </td>

                    <td align="center" id="guide"><input type="checkbox" name="guide:ca:CATALAN " id="guide:ca" value="Y" class="check"  /></td>

                    <td align="center" id="audio"><input type="checkbox" name="audio:ca:CATALAN " id="audio:ca" value="Y" class="check"  /></td>
 

                    <td><input type="hidden" name="srvLang" id="srvLang" value="CATALAN "></>

                        

                </tr>

</table>

Open in new window

0
Comment
Question by:vlad_oz
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22773447


$('#languagesList input:checked').each(function(){

  alert(this.name);

});

Open in new window

0
 

Author Comment

by:vlad_oz
ID: 22773521
Thanks for the quick responce sh0e!

I should explain a little more what I'm after:
What I want to be able to construct from the table above is something like the snippet below. So, I need to actually go though the table rows instead of just the checkbox elements as you suggested. Grab the first cell element name, then check if any checkboxes have been selected, if yes then I'll add that element name and whatever checkbox id (audio and/or guide) to the <UL></UL>

thanks again!
<ul id="selectedLanguageServices">

                                

                                    <li style="width:200px" id="serviceLanguage_zh">
 

                                        CHINESE &nbsp;

                                        Guide&nbsp;

                                        

                                    </li>

                                

                                    <li style="width:200px" id="serviceLanguage_tl">

                                        FILIPINO &nbsp;

                                        Guide&nbsp;

                                        Audio

                                    </li>
 

                                

                                    <li style="width:200px" id="serviceLanguage_vi">

                                        VIETNAMESE &nbsp;

                                        Guide&nbsp;

                                        

                                    </li>

                                

                            </ul>

Open in new window

0
 
LVL 16

Accepted Solution

by:
sh0e earned 500 total points
ID: 22773846

$('#languagesList tr').each(function(){

	var li;

	$('input:checked',this).each(function(){

		var name = this.name.split(':');

		if(!li)

			li = $('<li style="width:200px" id="serviceLanguage_' + name[1] + '">\n' + name[2] + '\n</li>');

		li.append(name[0].charAt(0).toUpperCase() + name[0].substr(1) + '\n');

	});

	$('#selectedLanguageServices').append(li);

});

Open in new window

0
 

Author Closing Comment

by:vlad_oz
ID: 31508613
sh0e, thanks very much for the solution!
worked the first time! very nicely done!
Cheers,
V
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now