-1

I am trying to display the checkboxes values like if the DB contains the following

 Array
(
    [0] => Array
        (
            [userid] => 868
            [username] => Pavanasri
            [firstname] => Pavana
            [email] => [email protected]
            [signupdate] => 2020-07-13
            [add_topic_priv] => Y
            [add_subject_priv] => Y
            [add_post_priv] => Y
            [add_note_priv] => Y
            [add_disable_priv] => N
            [add_notifications_priv] => Y
        )

    [1] => Array
        (
            [userid] => 785
            [username] => sam123
            [firstname] => sam
            [email] => [email protected]
            [signupdate] => 2020-07-23
            [add_topic_priv] => Y
            [add_subject_priv] => Y
            [add_post_priv] => Y
            [add_note_priv] => Y
            [add_disable_priv] => N
            [add_notifications_priv] => Y
        )
)

So here I have some fields fetching from the DB and first 5 fields are displaying in the table . But coming to the checkbox values how can I display the checked values [y] on the front end using jquery. Here is the jquery code.

  window.fillData = function(){
  alert("memberslist");
   var gid = $("#proid").val(); 
      $.ajax({
        type:"POST",
        dataType:"json",
        url:"memberslist.php",
        data:{
          gid:gid,
          action:"membersList",
         },
         success : function(response)
        { 
          
          if(response.status=="1")
          {
            response.data.forEach(function(item) {

              if (item.add_topic_priv =="Y")
            {
              $("#topic").is(":checked");
            }
             
            var row = "<tr>";
            row += "<td id=tid>" + item.username + "</td>";
            row += "<td id=firstname>" +item.firstname+ "</td>";
            row += "<td id=email>" + item.email +   "</td>";
            row += "<td id=signupdate>" + item.signupdate + "</td>";
            row += "<td id=invitedon>" + item.invitedon + "</td>";
            row += "<td id=acceptedon>" + item.acceptedon + "</td>";


            row += "<td> <input type=checkbox id=topic name=example2 class=td-check-box> </td>";
            row += "<td> <input type=checkbox id=subject name=example2 class=td-check-box> </td>";
            row += "<td> <input type=checkbox id=post name=example2 class=td-check-box> </td>";
            row += "<td> <input type=checkbox id=note name=example2 class=td-check-box> </td>";
            row += "<td> <input type=checkbox id=disable name=example2 class=td-check-box> </td>";
            row += "<td> <input type=checkbox id=notification name=example2 class=td-check-box> </td>";
            row += "<td><i id=deletep class=\'fa fa-trash-o\'></i></td>";
            row += "</tr>";
            $("#tableid").append(row);
           
            })
          }
        },
            error : function(response){
              $("#succ_msg").html("<p style=\'color:red;font-weight:bolder;\'>error</p>");
            }
          });
          return false;
        }

In the above code i am trying to print the fields but coming to the checkboxes it is not display checked for the id of "topic" . How can we fetch checked value to the html through jQuery. So I can Apply to other fields.

Output what i want enter image description here:

0

1 Answer 1

1

To check

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

To uncheck

$("#topic").prop("checked", false);

EDIT [contains code for something that was asked later on]

          if(response.status=="1")
          {
            
            response.data.forEach(function(item, index) {

            ....

            row += "<td> <input type=checkbox id=topic" + index +  "name=example2 class=td-check-box> </td>";

            ....

            $("#tableid").append(row);

              if (item.add_topic_priv =="Y")
            {
              $("#topic" + index).is(":checked");
            }
           
            })
          }
        },
Sign up to request clarification or add additional context in comments.

14 Comments

i have a dynamic values then how can i get the checked if i have Y and if have N then unchecked
i need to use the if stamtement right then how can i get that
if(item.add_topic_priv == "Y") { $("#topic").prop("checked", true); } else{ $("#topic").prop("checked", false); }@Aashish gaba is this is the correct way'
Yes, this is correct.
Did this work or not?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.