Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  134] [ 6]  / answers: 1 / hits: 38845  / 14 Years ago, mon, april 26, 2010, 12:00:00

Hi I'd like to get the list of selected checkboxes ina page, actually what I really need is th get the text of the element next to the checkbox which is a html element <li>
the code is below and its not working



Here is my current jQuery:



$(document).ready(function () {
$('#target').click(function () {
alert(in);
var checkValues = [];
$('input[name=checkboxlist]:checked').each(function() {
alert($(this)val());
checkValues.push($(this)val());
});
});
});


Here is the HTML:



<ul id=7b1fe2bd-1b26-4185-8cd9-aec10e652a70>
<li>Structured Products<input type=checkbox name=checkboxlist</li>
<li>Global FID
<ul>
<li>PowerPoint Presentations<input type=checkbox name=checkboxlist</li>
<li>Global Deck
<ul>
<li>Test1<input type=checkbox name=checkboxlist</li>

<li>Test2<input type=checkbox name=checkboxlist</li>
<li>Test3<input type=checkbox name=checkboxlist</li>

</ul>
<input type=checkbox name=checkboxlist</li>
<li>Credit Default Swaps Position
<ul>
<li>Test4<input type=checkbox name=checkboxlist</li>

<li>Test5<input type=checkbox name=checkboxlist</li>

</ul>
<input type=checkbox name=checkboxlist</li>
<li>Thought Leadership<input type=checkbox name=checkboxlist</li>
<li>Fixed Income Perspectives<input type=checkbox name=checkboxlist</li>
<li>Public Policy Information and Regulatory<input type=checkbox name=checkboxlist</li>

<li>Regional FID<input type=checkbox name=checkboxlist</li>

</ul>
<input type=checkbox name=checkboxlist</li>
<li>Global Rates<input type=checkbox name=checkboxlist</li>
<li>Global Credit Products<input type=checkbox name=checkboxlist</li>
<li>FX<input type=checkbox name=checkboxlist</li>

<li>Emerging Markets<input type=checkbox name=checkboxlist</li>
<li>Commodities<input type=checkbox name=checkboxlist</li>
<li>testcat<input type=checkbox name=checkboxlist</li>
<li>testcat<input type=checkbox name=checkboxlist</li>

</ul>

More From » jquery

 Answers
14

Your inputs need to be closed with a /> on the end to make your HTML valid, like this:



<input type=checkbox name=checkboxlist />


Then you can do jQuery like this to get an array of the text:



$(function () {
$('#target').click(function () {
var checkValues = $('input[name=checkboxlist]:checked').map(function() {
return $(this).parent().text();
}).get();
//do something with your checkValues array
});
});​


You can see this working here



Since all you need is the parent's .text() with your layout, you can use .map() to quickly get an array of a property based on the selector.


[#96969] Friday, April 23, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irvingcarloe

Total Points: 677
Total Questions: 109
Total Answers: 96

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
irvingcarloe questions
Wed, Mar 31, 21, 00:00, 3 Years ago
Tue, Aug 4, 20, 00:00, 4 Years ago
Fri, Jul 3, 20, 00:00, 4 Years ago
;