I am dynamically creating checkboxes using jquery and then appending the checkboxes inside a div tag. Now, I want to get the values of the checkboxes when I click on a button but for some reason, I am getting only "on" instead of the actual value. What could I be doing wrong ?
HTML:
<div class="checkboxes">
<label>Select a category</label><br>
</div>
<button style="background-color: #e7e7e7; color: black; border: none;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;" id="clr_btn">Clear
</button>
JavaScript:
$(document).ready(function () {
$.ajax({
url: "/getcategory",
type: "GET",
success: function (result) {
var result = result.results;
if (result.length > 0) {
$.each(result, function (i, res) {
var $chk = $('<input type="checkbox" class="chklist" name="chke" id="chk_'+i+'" val ='+res.name+'/>'+res.name+"<br />");
$('.checkboxes').append($chk)
});
}
},
error: function (error) {
}
});
})
// button click event
$('#clr_btn').click(function (e) {
alert($("input[name=chke]:checked").map(
function () {return this.value;}).get().join(","));
})
clickevent, but where is the button?$(this).val()work?