1

Inside my razor view, there is some text with corresponding CheckBox. That CheckBox has an id value. I want to send this id value to the MVC controller.

My simplified view has generated content like this:

<input id="14" class="imgCheckbox" type="checkbox">
<input id="deleteImgBtn" class="deleteImagesBtn" 
       type="button" value="Delete" name="deleteImgBtn">

Inside my javascript, I'm trying to gather all checked items and store them inside array which will be sent to the controller, again simplified:

var imgList = [];
$(document).on("click", "#deleteImgBtn", function (e) {
    e.preventDefault();
    $('.imgCheckbox:checked').each(function () {                    
        var id = $(this).attr('id');
        alert(id);
        imgList.push(id);
    });
...

On alerting id variable, I'm getting object Object. I don't see what I'm doing wrong and how to see if checked id value is stored inside the imgList array.

2
  • "again simplified" - So is the "object Object" alert you mention happening with the simplified code as shown or with your real code? Commented Dec 16, 2013 at 20:43
  • with this simplified function Commented Dec 16, 2013 at 20:44

1 Answer 1

1

You need to close the input tags in the markup like this:

<input id="14" class="imgCheckbox" type="checkbox" />
<input id="deleteImgBtn" class="deleteImagesBtn" 
type="button" value="Delete" name="deleteImgBtn" />

Please see this working fiddle:

http://jsfiddle.net/jM5e9/1/

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

2 Comments

Why would you need to close input tags with /? Does asp force you to use xhtml? Html5 and html4 don't require self-closing input elements.
@nnnnnn It depends what is specified in the _layout.cshtml, you can change it to anything.

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.