in jquery can i add a readonly attribute to a input that the only property i can touch is a function? -
i have multiple inputs (unknow number)
<input id="ctl00_contentplaceholder_txtsopurchasersapid" onkeydown="return scriptreadonly();" style="width: 150px" name=ctl00$contentplaceholder$txtsopurchasersapid>
there not on same page of app, id's , names dynamically loaded cannot add class because there many , don't know location of inputs.
my question is, can attach attribute of readonly jquery inputs thing same on each of them onkeydown="return scriptreadonly();
yes, can, using attribute equals selector:
$('input[onkeydown="return scriptreadonly();"]').prop("readonly", true);
live example:
function scriptreadonly() { } $('input[onkeydown="return scriptreadonly();"]').prop("readonly", true);
<input id="ctl00_contentplaceholder_txtsopurchasersapid" onkeydown="return scriptreadonly();" style="width: 150px" name=ctl00$contentplaceholder$txtsopurchasersapid> <input id="ctl01_contentplaceholder_txtsopurchasersapid" onkeydown="return scriptreadonly();" style="width: 150px" name=ctl01$contentplaceholder$txtsopurchasersapid> <input id="ctl02_contentplaceholder_txtsopurchasersapid" onkeydown="return scriptreadonly();" style="width: 150px" name=ctl02$contentplaceholder$txtsopurchasersapid> <input id="ctl03_contentplaceholder_txtsopurchasersapid" onkeydown="return scriptreadonly();" style="width: 150px" name=ctl03$contentplaceholder$txtsopurchasersapid> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
if text of onkeydown
attribute varies bit, can use filter
instead make things more fuzzy:
$('input').filter(function() { return $(this).attr("onkeydown").indexof("scriptreadonly") != -1; }).prop("readonly", true);
live example:
function scriptreadonly() { } $('input').filter(function() { return $(this).attr("onkeydown").indexof("scriptreadonly") != -1; }).prop("readonly", true);
<input id="ctl00_contentplaceholder_txtsopurchasersapid" onkeydown="return /*0*/scriptreadonly();" style="width: 150px" name=ctl00$contentplaceholder$txtsopurchasersapid> <input id="ctl01_contentplaceholder_txtsopurchasersapid" onkeydown="return /*1*/scriptreadonly();" style="width: 150px" name=ctl01$contentplaceholder$txtsopurchasersapid> <input id="ctl02_contentplaceholder_txtsopurchasersapid" onkeydown="return /*2*/scriptreadonly();" style="width: 150px" name=ctl02$contentplaceholder$txtsopurchasersapid> <input id="ctl03_contentplaceholder_txtsopurchasersapid" onkeydown="return /*3*/scriptreadonly();" style="width: 150px" name=ctl03$contentplaceholder$txtsopurchasersapid> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Comments
Post a Comment