Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
script:-
JavaScript
$(function () {
               $(".checkBoxClass").click(function (e) {
                   var CustomerIDArray=[];
                   $('#tblEmailScheduler').find('input[type="checkbox"]').each(function (i,item) {
                       if($(item).prop('checked'))
                           CustomerIDArray.push(item.Id);
                       alert(item.id);
                   });

View:-
HTML
<pre><table id="tblEmailScheduler"  class="table-bordered col-offset-12">
                <thead>
                    <tr class="label-primary">
                        <th style="padding:5px 15px;">
                            First Name
                        </th>
                        <th style="padding:5px 15px;">
                            Last Name
                        </th>
                        <th style="padding:5px 15px;">
                            Email ID
                        </th>
                        <th style="padding:5px 15px;">
                            Customer Type
                        </th>
                        <th style="padding:5px 15px;">
                            Customer Designation
                            @Html.DropDownList("CustomerDesignation", new SelectList(ViewBag.SelectAllCustomerDesignationDDL, "Value", "Text"), new { id = "CustomerDesignationDDL" , name = "CustomerDesignationDDL" })
                        </th>
                        <th style="padding:5px 15px;">
                            Select All
                            <div class="checkbox control-group">
                                <label>
                                    <input type="checkbox" id="cbSelectAll" />
                                </label>
                            </div>
                        </th>

                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <th colspan="2">
                            EmailTemplate :
                            @Html.DropDownList("EmailSubject", new SelectList(ViewBag.SelectAllEmailTemplateDDL, "Value", "Text"), new { id = "SelectAllEmailTemplateDDL" })
                        </th>
                        <th colspan="2">
                            Set Date And Time:
                            <input type="text" class = "from-date-picker" readonly = "readonly"  />
                        </th>
                        <th colspan="2">
                           <input type="submit" value="Schedule" id="btnSubmit" class="btn btn-default" />
                        </th>
                        <td>

                        </td>
                    </tr>
                </tfoot>
                @foreach (var item in Model)
                {
                    <tr style="text-align:center">
                        <td id="tblFirstName">
                            @item.FirstName
                        </td>
                        <td id="tblLastName">
                            @item.LastName
                        </td>
                        <td id="tblEmailID">
                            @item.EmailID
                        </td>
                        <td id="tblCustomerType">
                            @item.CustomerType
                        </td>
                        <td id="tblCustomerDesignation">
                            @item.CustomerDesignation
                        </td>
                        <td>
                            <div class="checkbox control-group">
                                <label>
                                    <input type="checkbox" id="cbCustomer_@item.CustomerID" value="@item.CustomerID" class="checkBoxClass"/>
                                </label>
                            </div>
                        </td>
                    </tr>
                }
          </table>


1.In this when in click on specific row checkbox then in alert it should show mw only checked ID but in this in alert it shows me all Id.
2.I want to add specific row in javascript when checked on checkbox.
3.then remove id from array when click on unchecked from checkbox


What I have tried:

JavaScript
$(function () {
                $(".checkBoxClass").click(function (e) {
                    var CustomerIDArray=[]; 
                    $('#tblEmailScheduler').find('input[type="checkbox"]').each(function (i,item) {
                        if($(item).prop('checked'))
                            CustomerIDArray.push(item.Id);
                        alert(item.id);
                        if($(item).prop('unchecked'))
                          CustomerIDArray.slice(item.Id);
                        alert(item.id);
                    });
                });
            });
Posted
Updated 26-Apr-17 4:43am

1 solution

var CustomerIDArray=[]; 
$(document).ready(function () {
	$(".checkBoxClass").click(function (e) {
		var arr=CustomerIDArray;
		var checkedId=$(this).attr('id');
		if($(this).prop('checked')){
			CustomerIDArray.push(checkedId);
			arr=CustomerIDArray;
		}
		else
		{
			jQuery.each(CustomerIDArray,function(i,item){
				if(arr[i] == checkedId) {
					arr.splice(i, 1);
				}
			});
			CustomerIDArray =arr;
		}
		var ids="";
			jQuery.each(CustomerIDArray,function(i,item){
				if(ids=="")
				{
					ids= CustomerIDArray[i];
				}
				else
				{
					ids= ids+ ","+   CustomerIDArray[i];
				}
			});
		alert(ids);
	});           
}); 
 
Share this answer
 
Comments
santosh_131 26-Apr-17 10:45am    
try the above script and see
Shridhar Salunkhe 27-Apr-17 1:01am    
thank you it worked!
Shridhar Salunkhe 27-Apr-17 1:01am    
can u tell me what to do for select all checkbox?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900