2

I have a HTML table which has the following structure:

<table id="myTable">
  <tr>
     <td><input type="text" name="FullName" value="Tom" /></td>
     <td><input type="checkbox" name="isActive" /></td>
     <td><a href="javascript:void(0);" class="edit">Edit</a>
  </tr>
</table>

When the user clicks the 'edit' link, a Javascript function is called (see below). In this function I need to get the data from the table, i.e., FullName and whether or not isActive has been checked.

 $("#namedTutors").on('click', '.editTutor', function () {
    var tr = $(this).closest("tr");
    var fullName = tr.find("input[name=FullName]").val();
});

I can get the FullName easy enough, but I'm having difficulties retrieving the data to see if isActive has been checked/ticked or not.

Could someone please help.

Thanks.

1
  • 1
    tr.find("input[name=FullName]").is(':checked') Commented Apr 2, 2019 at 10:05

2 Answers 2

2

You could select the ckeckbox input by name [name=isActive] then use the .is(':checked') to check whether the ckeckbox is checked or not, like:

$("#namedTutors").on('click', '.editTutor', function() {
  var tr = $(this).closest("tr");
  var fullName = tr.find("input[name=FullName]").val();
  var isActive = tr.find("input[name=isActive]").is(':checked');
  
  console.log( isActive ); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="namedTutors">
  <tr>
    <td><input type="text" name="FullName" value="Tom" /></td>
    <td><input type="checkbox" name="isActive" /></td>
    <td><a href="javascript:void(0);" class="editTutor">Edit</a>
  </tr>
</table>

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

1 Comment

I really appreciate your time and answer. This has helped massively. Thanks.
2
if(tr.find('input[name="isActive"]:checked').length) {
     console.log('it is checked');
}

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.