Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5020
  • Last Modified:

iterating through table with jquery

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
vlad_oz
Asked:
vlad_oz
  • 2
  • 2
1 Solution
 
sh0eCommented:


$('#languagesList input:checked').each(function(){
  alert(this.name);
});

Open in new window

0
 
vlad_ozAuthor Commented:
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
 
sh0eCommented:

$('#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
 
vlad_ozAuthor Commented:
sh0e, thanks very much for the solution!
worked the first time! very nicely done!
Cheers,
V
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now