2

I have data coming in json format like this

        {
           feild: ...
           ...
            "transport_details": [
                       {
                       "allotment_id": ObjectId("5b755710d2ccda0978005d6e"),
                       "status": "Active"
                       },
                       {
                         "allotment_id": ObjectId("5b755710d2ccda0978005d6e"), 
                        "status": "Inactive"
                       }
                    ]
        }

I have written below java script function like

            function checkIfInActive(transportAllotment, id)
        {
            if (transportAllotment.length == 0)
            {
                   return  '<div class="checkbox checkbox-success">' +
                        '<input class="commoncheckbox" type="checkbox" id="studentId_-' + id + '' +
                        '" name="studentId_-' + id + '" value="' + id+ '"' +' >' + '<label></label></div>';
            }
            else
            {
                 var max = transportAllotment.length-1;

                 var status = transportAllotment[max]["status"];

                 if(status != "Active")
                 {
                      return  '<div class="checkbox checkbox-success">' +
                        '<input class="commoncheckbox" type="checkbox" id="studentId_-' + id + '' +
                        '" name="studentId_-' + id + '" value="' + id+ '"' +' >' + '<label></label></div>';
                 }
                 else
                 {
                     return '<div class="checkbox"><input class="disabled-check" type="checkbox" disabled><label></label></div>';
                 }
            }
        }

I am trying to return html with class commoncheckbox , if the status of last element of sub array transport_details is Inactive. The above is not returning checkbox with class commoncheckbox in such cases.

Please help!!!

1
  • You've to add checked attribute to the checbox you want to enable Commented Aug 17, 2018 at 6:25

2 Answers 2

1

var myObj = {
    "aaa":"zzzz",
    "bbb":23,
    "transport_details":[ 
    	{
				"status": "Active"
    	},
      {
      	"status": "Inactive"
      }
    ]
 } 


var status = myObj.transport_details[myObj.transport_details.length - 1].status;

var html = (status === "Active")? '<input type="checkbox">' : '<input type="checkbox" disabled>' ;

$('#banner-message').html(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div id="banner-message">
 
</div>

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

Comments

1

If status is inactive it will go inside this block. Seems the current html literals is messed up a bit. You can use template literals which starts with backtck (`) not to confuse with quotes

if (status != "Active") {
  return `<div class="checkbox checkbox-success">
          <input class="commoncheckbox" type="checkbox" id="studentId_${id}"
          name="studentId_-${id}"
          value="${id}">
          <label></label>
          </div>`;
}

2 Comments

i am sorry i dont need any checked checkboxes at any condition... i am talking about html returning
@NidaAmin what does enabled checkbox mean

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.