Solved

iterating through table with jquery

Posted on 2008-10-21
4
5,005 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
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…

690 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