Solved

iterating through table with jquery

Posted on 2008-10-21
4
4,985 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where is java script information saved? 2 50
how can i test my code? 1 34
Getting selected value of a dropdown with jquery 7 34
JS does not refresh 6 16
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

776 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