/    Sign up×
Community /Pin to ProfileBookmark

Append event to current button event

I am trying to append a function to a previously defined button onclick event.

The following does not work, but it doesn’t give me any errors to follow-up on either.

Can anyone give me a clue as to what I am obviously doing wrong?

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> Append onclick function </title>
</head>
<body>

<input type=”button” value=”Button 1″ onclick=”alert(this.value)”>
<input type=”button” value=”Button 2″ onclick=”alert(this.value)”>
<input type=”button” value=”Button 3″ onclick=”alert(this.value)”>
<input type=”button” value=”Button 4″ onclick=”alert(this.value)”>
<input type=”button” value=”Button 5″ onclick=”alert(this.value)”>
<pre id=”debug”></pre>

<script type=”text/javascript”>
function appendClick(msg) { // dummy function to prove function is appended to event
document.getElementById(‘debug’).innerHTML = msg;
}

function init() {
var tmp = ”,
sel = document.getElementsByTagName(‘input’), // collection of buttons
currentOnClick,
currentValue;
for (var i=0, len=sel.length; i<len; i++) { // check button collection contents
currentOnClick = sel[i].onclick; // save current onclick event
currentValue = sel[i].value; // remember current value of button
sel[i].onclick = function () { // re-assign onclick event
currentOnClick(); // perform original event
appendClick(currentValue); // append new event function
}
}
}
init();

</script>

</body>
</html>
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@TcobbMar 11.2015 — Closures and scope.

At first I could get the text to appear in the PRE tag, but no matter what button I pushed

it read 'Button 5' --that indicated a closure problem. So, in your for loop:

[CODE] for (var i=0, len=sel.length; i<len; i++) { // check button collection contents
currentOnClick = sel[i].onclick; // save current onclick event
currentValue = sel[i].value; // remember current value of button
sel[i].onclick = function(currentOnClick,appendClick,currentValue){
return function(e){

currentOnClick(e); // perform original event
appendClick(currentValue); // append new event function
};
}(currentOnClick,appendClick,currentValue);
}[/CODE]


That fixed the closure problem.

However, the initial alert (given by the original inline onclick function) always read 'undefined.' The problem is that the original alert is for 'this.value' which is fine when it is the actual onclick function. But when you stick the function inside another function, as you do, the 'this' no longer refers to the button, but rather to the Window, which has no value, so you get undefined. If you change:

[CODE]<input type="button" value="Button 5" onclick="alert(this.value)">[/CODE]

to

[CODE]<input type="button" value="Button 5" onclick="alert(this)">[/CODE]

it will demonstrate this.
Copy linkTweet thisAlerts:
@KeverMar 11.2015 — You can call a function with a given 'this' keyword by using the 'call' method on a function. Using the bind method, you can create a new function with a given 'this' and given arguments.
function init() {
var tmp = '',
sel = document.getElementsByTagName('input'), // collection of buttons
currentOnClick,
currentValue;
for (var i=0, len=sel.length; i&lt;len; i++) { // check button collection contents
currentOnClick = sel[i].onclick; // save current onclick event
currentValue = sel[i].value; // remember current value of button
sel[i].onclick = function (currentOnClick, currentValue) { // re-assign onclick event
currentOnClick.call(this); // perform original event
appendClick(currentValue); // append new event function
}.bind(sel[i], currentOnClick, currentValue); <br/>
}
}
Copy linkTweet thisAlerts:
@JMRKERauthorMar 12.2015 — Thank you both.

It certainly gives me more material for study.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...