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>
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.
$("input[type=checkbox]:checked").parent().text()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With