mercredi 12 juillet 2017

JQUERY Add checked attribute to checkbox depending on Table column value `Status` (1 or 0)

I've been googling really hard on this but putting the keywords checkbox and table together gives me results of "getting row values of checked row".

And I've landed to rare questions with the same concerns but it is all advanced (ajax, angularjs) I'm just a student-level programmer(basic jquery at most).

so I have this: So what it does is it takes the values of the cell and puts it into textbox, no problem there but I want to take the value of status which is 1 (for active) and put it into a checkbox, if the value is 1 the checkbox will be checked, and if 0 it will not be checked.

I thank you all in advance, and please forgive the student-y coding, I'd appreciate any corrections though, although my understanding is limited, I'm continuously trying to widen my knowledge.

$(document).ready(function(){
        $(".buttonedit").click(function(){
                var Lastname = $(this).closest("tr").find("td:eq(0)").text();
                var Firstname = $(this).closest("tr").find("td:eq(1)").text();
                var Status = $(this).closest("tr").find("td:eq(2)").text();
    $("#lname").val(Lastname);
    $("#fname").val(Firstname);
    
        });
});
<script src="http://ift.tt/1oMJErh"></script>
<table border="solid">
  
  <thead>
    <tr>
      <td>lastnmae</td>
      <td>firstname</td>
      <td>status</td>
      <td>action</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>gates</td>
      <td>bill</td>
      <td>1</td>
      <td><button class="buttonedit">Edit</button></td>
    </tr>
    
  </tbody>
</table>

<form action="">
  <input type="text" placeholder="enter lastname"id="lname"><br>
  <input type="text" placeholder="enter firstname"id="fname"><br>
  <input type="checkbox" name="status" id="status">Active?<br>
</form>



Aucun commentaire:

Enregistrer un commentaire