1

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(","));

    })
8
  • Please post all of the relevant code so we can replicate your issue. You mention a "button's" click event, but where is the button? Commented Oct 8, 2018 at 18:29
  • 1
    Can you try to get the value with the "this.val()" instead of "this.value"? Commented Oct 8, 2018 at 18:32
  • @HugoESachez:- error - this.val() is not function Commented Oct 8, 2018 at 18:35
  • @RemyPhrentch $(this).val() work? Commented Oct 8, 2018 at 18:36
  • 1
    @HugoESachez:- Yes you are right. I was using val instead of value and also not using proper delimeters. Thank you so much Commented Oct 8, 2018 at 18:48

1 Answer 1

1

In your HTML literal string, you weren't setting the value of the elements, you were setting the val attribute, which doesn't exist.

// Dummy variable to make loop run
let result = [{name:"foo"},{name:"bar"},{name:"baz"}];

$.each(result, function (i, res) {
  var $chk = $('<input type="checkbox" class="chklist" name="chke"'  
                + 'id="chk_' + i + '" value =' + res.name + '>' + 
                res.name + "<br />");
  $('.checkboxes').append($chk)
});

// JQuery recommends using ".on()" to bind events now:
$('#clr_btn').on("click", function (e) {
  alert($("input[name='chke']:checked").map(
    function () {return this.value;}).get().join(","));
});
#clr_btn {
        background-color: #e7e7e7; 
        color: black; border: none;
        padding: 15px 32px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkboxes">
  <label>Select a category</label><br>
</div>

<!-- Don't use inline CSS. -->
<button id="clr_btn" type="button">Clear</button>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.