Broow Broow - 3 months ago 12
Javascript Question

How to put image inside a button in JS or how to disabled a image button when condition is meet?

I have this code in my JQgrid to create a button inside a grid

afterInsertRow: function (rowid) {
var obj = jQuery("#FiTATimeCorrectionV2List").getRowData(rowid);
var FADTLSID = obj.FitaAssignDtlID;
if (FADTLSID !== undefined) {
if (FADTLSID !== "") {
var btnApprove = "<img alt='' src='../../Content/Images/newimages/check.png' style='height:20px;width:20px;' style ='width: 90px' id='btnApprove" + rowid + "' onclick='clickmeapproved(" + rowid + " )' />"
var btnDisApprove = "<img alt='' src='../../Content/Images/newimages/delete.png' style='height:20px;width:20px;' style ='width: 90px' id='btnApprove" + rowid + "' onclick='clickmedisapproved(" + rowid + " )' />"
jQuery("#FiTATimeCorrectionV2List").setRowData(rowid, { FitaCorForApproval: btnApprove });
jQuery("#FiTATimeCorrectionV2List").setRowData(rowid, { FitaCorForDisApproval: btnDisApprove });
var temp = obj.FitaStatus;
if (temp == "Approved") {
$("#btnApprove" + rowid).attr("disabled", true);
$("#btnDisApprove" + rowid).attr("disabled", true);
} else {
$("#btnApprove" + rowid).attr("disabled", false);
$("#btnDisApprove" + rowid).attr("disabled", false);
}
}
}
},


That kind of code is in image format however the code for disable is not working if I change to
<input type = 'button'
the code is working and the image disappear. I just want to create a image button which if a condition has been meet the image button is disabled. What I want is after the data loaded the image button/s will be
disabled
or
enabled
if the condition is meet. As code above when the
Status
is
Approved
or
Disapproved
I want to disabled all the image button/s.

Answer

Use jQuery's .prop method.

$("button:eq(0)").attr("disabled", false);
$("button:eq(1)").attr("disabled", true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button>1. button</button>
<button>2. button</button>