The one approach I can think of that doesn't require changing the HTML you're working with, but does involve changing the DOM you receive, is:
function x() {
$(this).prop('disabled', true);
}
// selecting, and iterating over, all <input> elements with
// an in-line 'onclick' event-handler attribute:
$('input[onclick]').each(function() {
// retrieving the contents of the 'onclick' attribute:
var onClickFunction = $(this).attr('onclick');
// setting the onClickFunction variable to the
// substring of the existing value from the first
// character to the index of the first '('
// character, so from 'x()' to 'x' or from
// 'function1();function2()' to 'function1'
// (this could be worked-around, but this is
// a simple proof-of-concept):
onClickFunction = onClickFunction.substring(0, onClickFunction.indexOf('('));
// if there is a function, or a variable, held as a
// property of the window object, then:
if (window[onClickFunction]) {
// we remove the 'onclick' attribute, and use the
// on() method to add the event-handler back:
$(this).removeAttr('onclick').on('click', window[onClickFunction]);
}
});
function x() {
$(this).prop('disabled', true);
}
$('input[onclick]').each(function() {
var onClickFunction = $(this).attr('onclick');
onClickFunction = onClickFunction.substring(0, onClickFunction.indexOf('('));
if (window[onClickFunction]) {
$(this).removeAttr('onclick').on('click', window[onClickFunction]);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='button' onclick='x()' value='Disable me' />
<input type='button' onclick='x()' value='Disable me' />
It's worth noting that, while the above is quite possible with jQuery, it's similarly possible with plain JavaScript:
function x() {
this.disabled = true;
}
// excuse the absurdly-long variable-names; they were meant to be
// descriptive.
// here we retrieve all <input> elements with an onclick attribute:
var inputsWithOnClick = document.querySelectorAll('input[onclick]'),
// here we use Array.from() to turn that collection into an Array:
arrayOfOnClickInputs = Array.from(inputsWithOnClick);
// iterating over the Array, 'input' (the first argument)
// refers to the current element of the array over which
// we're iterating:
arrayOfOnClickInputs.forEach(function(input) {
// retrieving the contents of the 'onclick' attribute:
var onClickFunction = input.getAttribute('onclick');
// updating that variable to contain the substring of
// of the attribute-value, from the first character to
// the first occurrence of of the '(' character:
onClickFunction = onClickFunction.substring(0, onClickFunction.indexOf('('));
// as above:
if (window[onClickFunction]) {
// removing the 'onclick' attribute:
input.removeAttribute('onclick');
// using EventTarget.addEventListener to bind the
// function to the click-event on the <input> element:
input.addEventListener('click', window[onClickFunction]);
}
});
function x() {
this.disabled = true;
}
var inputsWithOnClick = document.querySelectorAll('input[onclick]'),
arrayOfOnClickInputs = Array.from(inputsWithOnClick);
arrayOfOnClickInputs.forEach(function(input) {
var onClickFunction = input.getAttribute('onclick');
onClickFunction = onClickFunction.substring(0, onClickFunction.indexOf('('));
if (window[onClickFunction]) {
input.removeAttribute('onclick');
input.addEventListener('click', window[onClickFunction]);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='button' onclick='x()' value='Disable me' />
<input type='button' onclick='x()' value='Disable me' />
The reason for these approaches is simply that, in this case, the attribute is known (onclick), and you wanted to use this within the function, which jQuery manages for you, and which addEventListener() also does automagically.
And I honestly couldn't think of an alternative approach to supply the this to where you needed it to be.
References: