0

I have an array of checkboxes. If there is a checked checkbox, it will be added into a string of text. Here is my code:

  var a = $(this).closest('tr').parent().closest('tr').find('input[type="checkbox"].CheckedArray');

        for (var i = 0; i < a.length; i++) {
            if (a[i].is(':checked')) {
                list= list+ a[i].value + ", ";
            }
            alert(list);
        }

        <table id="" class="table">
<tr>
    <td>
        <div class="form-horizontal form-inline">
            <input class="CheckedArray" type="Checkbox" value="' + Id + '" id="CheckedArray" name="CheckedArray" />Select<div class="form-group"><label for="" class="control-label col-sm-1"></label><label for="" class="control-label col-sm-4">PD No: </label><div class="col-sm-3"><input name="PDNo" class="form-control input-sm " id="PDNo" type="text" /></div></div>
        </div>
    </td>
</tr>

I used checked as condition but it doesnt show any alert. I dont want to use filter function because i need the iteration value to shows other input.

3
  • can you add your HTML? which includes the table Commented Feb 11, 2019 at 3:57
  • I am using jquery to define the input. but I have edited the question Commented Feb 11, 2019 at 3:59
  • "I have an array of checkboxes." An array of checkboxes usually implies more than one checkbox, yet I see only one checkbox called #CheckedArray? Commented Feb 11, 2019 at 4:13

3 Answers 3

2
if ( $( elem ).prop( "checked" ) )
{
  \\Code_here
}

Or

if ( $( elem ).is( ":checked" ) )
{
  \\Code_here
}

Both will work.Try adding $ sign in your code.

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

Comments

1

You got the location for checked checkbox but you will never get the element if you are not using $ sign..

Use the below code and everything will work out.

$(a[I]).prop("checked");

4 Comments

it works! thank you. i just want to ask, if i want to assign a text into an input located at the same row as checked checkbox, i use the same code but i didnt works? $(b[i]).val("hello");
You can use the "closest" or "find" attribute to get the element which is next to check box. And then assign the text.... For example. $(a[i]).find(":input").Val("your text box");
Let me know your input type which is next to your check box.. so that I can send you the exact code....
The input is PDNo. It is stated in the question. What I actually want to do is, the first row of checked checkbox input need to be keyed in by user. But if the user check another checkbox, the input on that particular row is auto filled using the first input value.
0

Could you use


  <script>
    $(document).ready(function()
    {
     $(".CheckedArray").change(function()
       {
         console.log( $(this).prop("checked"),$(this).attr("id") );

       });

    });
     </script>

<table id="" class="table">
<tr>
<td>
<div class="form-horizontal form-inline">
    <input class="CheckedArray" type="Checkbox" value="' + Id + '" id="CheckedArray" name="CheckedArray" />Select<div class="form-group"><label for="" class="control-label col-sm-1"></label><label for="" class="control-label col-sm-4">PD No: </label><div class="col-sm-3"><input name="PDNo" class="form-control input-sm " id="PDNo" type="text" /></div></div>
</div>
</td>
</tr>

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.