0

I have a form on a page, with a checkbox input field. I also have a table on the page with a column that has true/false values. Each row in the table also has a checkbox with id="selectedReviewTypeYear". There is also a button with id="getDataToEdit".

When the getDataToEdit button is clicked, the javascript checks to see if any id="selectedReviewTypeYear" boxes are checked. If a box is checked, a div is opened up with form fields prepopulated based on the row selected. If no boxes are checked, the div remains hidden.

My problem is that the checkboxes aren't getting checked when the table data is "true." Here is the javascript:

 $('#getDataToEdit').on('click', function (){ $('#reviewTypeTable').find('tr').each(function (){ var row = $(this); if (row.find('input[id="selectedReviewTypeYearID"]').is(':checked')){ var idx = table.row(this).index(); var vReviewName = document.getElementById("editReviewTypeYear-name"); var vAllowMultiple = document.getElementById("allowMultiple"); var vAllowMultipleSpan = document.getElementById("allowMultipleSpan"); vReviewName.innerHTML = table.cell(idx, 2).data(); if (table.cell(idx, 3).data() == "true"){ $("#allowMultiple").prop("checked", true); vAllowMultipleSpan.innerHTML = table.cell(idx, 3).data(); } else { $("#allowMultiple").prop("checked", false); vAllowMultipleSpan.innerHTML = table.cell(idx, 3).data(); } showHide.style.visibility = 'visible'; } }) }); 

For testing purposes, I added a span element to the form called allowMultipleSpan. This was simply so I could see if the javascript is able to read the data in the table field, which it is. When I select a row, then click the button, the div opens up with the vReviewName.innerHTML populated correctly as well as the allowMultipleSpan populated correctly. However, the checkbox is not getting checked if the data is "true".

I am running JQuery 2.1.1. So far, I have tried using the following variations of the script, all with the same result:

From the JQuery (1.6+) spec:

$("#allowMultiple").prop("checked", true);

From the jQuery (1.5-) spec:

$("#allowMultiple").attr("checked", true);

From the Javascript spec:

document.getElementById("allowMultiple").checked = true;

Here is the HTML for my form:

<div class="widget-header"><h3>Edit: <span id="editReviewTypeYear-name" style="color: blue"></span></h3></div> <div class="widget-content"> <form id="editReviewTypeYear-form" class="form-horizontal" url="submitReviewTypeYear.htm"> <div class="form-group"> <label class="col-sm-2 control-label">Allow Multiple: </label> <div class="col-sm-10"> <input type="checkbox" id="allowMultiple" name="allowMultiple" /> <span id="allowMultipleSpan" style="color: blue"></span> </div> </div> <div class="form-actions"> <button id="submitReviewTypeYear" type="submit">Save</button> <button class="btn btn-small btn-warning reg-cancel" id="submitReviewTypeYear-cancel" type="button" name="Cancel" value="cancel">Cancel</button> </div> </form> </div> 
5
  • 1
    Have you tried if (table.cell(idx, 3).data() == true)? Commented Jan 13, 2016 at 15:17
  • Where's the HTML for getDataToEdit and reviewTypeTable? Post a complete code example please. Also, what errors in the console do you get? Commented Jan 13, 2016 at 15:19
  • @LucasRodrigues - aaaaand that was it. Any idea why that if test would change the result? Like I said, the span was still being populated with the correct value, so from all indications it appeared as though the if test was firing off correctly. Commented Jan 13, 2016 at 15:21
  • @j08691 - none of that is necessary. I provided all relevant code. Commented Jan 13, 2016 at 15:22
  • And there were no errors in the console. Commented Jan 13, 2016 at 15:23

1 Answer 1

1

Have you tried if (table.cell(idx, 3).data() == true)?

You were always getting the false condition because you were comparing a boolean with "true" as a string.

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

1 Comment

Oooooh, right. Data types. "I always mess up some mundane detail!" Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.