javascript - Changing input box also need to find check box checked or not in JS -


i have input box along checkbox in table <td> below,

<td>     <input class="comment" type="text" data-db="comment" data-id="{{uid}}"/>     <input type="checkbox" id="summary" title="check set summary" /> </td> 

based on check box content of input box stored in db.

in js file, tried like

var updatecomment = function( eventdata ) {     var target = eventdata.target;     var dbcolumn = $(target).attr('data-db');     var api = $('#api').val();     var newvalue = $(target).val();     var rowid = $(target).attr('data-id');     var summary = $('#summary').is(':checked');     params = { "function":"updatecomments", "id": rowid, "summary": summary };     params[dbcolumn] = newvalue;     jquery.post( api, params); };  $('.comment').change(updatecomment); 

but var summary returning false.

i tried many ways prop('checked'),(#summary:checked).val() returning false only.

how solve problem?

looks have multiple rows of checkboxes + input fields in table. doing $('#summary').is(':checked') return value of first matching element since id in dom should unique.

so, modify code this:

<td>     <input class="comment" type="text" data-db="comment" data-id="{{uid}}"/>     <input type="checkbox" class="summary" title="check set summary" /> </td> 

and, instead of $('#summary').is(':checked'); can write this:

var summary = $(target).parent().find(".summary").is(':checked'); 

by doing this, making sure checking value of checkbox selected input field only.

update: listening on both conditions i.e. when when checking checkbox first , typing input box , when first typing input box , checked:

register change event checkbox:

// whenever user changes checkbox $(".summary").change(function() {     // trigger "change" event in sibling input element     $(this).parent().find(".comment").trigger("change"); }); 

Comments