Ibanez1700
Ibanez1700

Reputation: 89

Hiding button does not work using jquery

I have a partial class in Asp MVC that goes like this:

<div class="table-responsive">
  <table class="table" id="tbl_visitors" data-animate="fadeInRight">
    <thead>
      <tr>
        <th>
          @Html.DisplayNameFor(model => model.VisitorId)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.TicketId)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.RequestStatus)
        </th>
        <th></th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      @foreach (var item in Model) {
      <tr>
        <td>
          @Html.DisplayFor(modelItem => item.VisitorId)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.TicketId)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.RequestStatus)
        </td>
        <td id="item_checkbox">
          @if (item.RequestStatus != "Pending") {
          <input [email protected] checked="checked" class="use-this switch-toggle switch-flat-mini switch-toggle-flat" type="checkbox">
          <label [email protected]></label>
          } else {
          <input [email protected] class="use-this switch-toggle switch-flat-mini switch-toggle-flat" type="checkbox">
          <label [email protected]></label>
          }
        </td>
        <th>
          <a href="javascript:void(0);" onclick="deletevisitor(@item.VisitorId, @item.TicketId ) ">Remove</a>
        </th>
      </tr>
      }
    </tbody>
  </table>
</div>
<div class="alignright" id="div_btn_approve">
  <button id="btn_approve_ticket" class="button button-border button-rounded button-green">Approve</button>
</div>

and a script to disable the button in the partial view in my "index" view like so:

function getVisitorsOfApprovedTicket(ticketId, requestedBy, schedule) {
  var url = '/Member/GetVisitorViaTicketId?ticketId=' + ticketId;
  $("#div_approved_ticket_visitors").load(url);
  $("#hidden_created_by").val(requestedBy);
  //$("#btn_approve_ticket").prop( "disabled", true );
  $("#btn_approve_ticket").hide();
}

When I try to hide it in my partial view by creating the script, it hides the button but why doesn't it work on my index view when I call the partial view? Can you please show me how to do it right? Thank you.

Upvotes: 0

Views: 47

Answers (1)

masterpreenz
masterpreenz

Reputation: 2290

Because .load is not finished yet and you try to .hide() an element that does not exist:

function getVisitorsOfApprovedTicket(ticketId, requestedBy, schedule) {
  var url = '/Member/GetVisitorViaTicketId?ticketId=' + ticketId;
  // this is a async process
  $("#div_approved_ticket_visitors").load(url);
  $("#hidden_created_by").val(requestedBy);
  //$("#btn_approve_ticket").prop( "disabled", true );
  // you are trying to hide an element that is not present yet.
  $("#btn_approve_ticket").hide();
}

If you want to hide an element that is inside this var url = '/Member/GetVisitorViaTicketId?ticketId=' + ticketId; view. You need to wait it load first before proceeding:

function getVisitorsOfApprovedTicket(ticketId, requestedBy, schedule) {
  var url = '/Member/GetVisitorViaTicketId?ticketId=' + ticketId;
  // this is a async process
  $("#div_approved_ticket_visitors").load(url, function () {
    // this function is called after its done
    // here you can safely hide the element
    $("#btn_approve_ticket").hide();
  });
  $("#hidden_created_by").val(requestedBy);
  //$("#btn_approve_ticket").prop( "disabled", true );
}

hope that helps

Upvotes: 2

Related Questions