In my sample HTML code i got some buttons with a default function to change the text and style on onclick
event:
<button onclick="changeText(this)">Hit Me!</button><br>
<button onclick="changeText(this)">Hit Me!</button><br>
<button onclick="changeText(this)">Hit Me!</button><br>
<button onclick="changeText(this)">Hit Me!</button><br>
<button onclick="changeText(this)">Hit Me!</button><br>
<script>
function changeText(id) {
id.innerHTML = "Ouch!";
id.style.color="red";
id.onclick= again(this);
}
function again(id) {
id.innerHTML = "Again!";
id.style.color=#FF0000;
}
</script>
I'm trying to change the onclick
event at the end of the default function:
id.onclick= again(this);
But it doesn't work;
This is the jsfiddle link
I've tried solutions in this question and this one
this:
id.onclick = function(this) { id.innerHTML = "Again!"; id.style.color=#FF0000; }
and this:
id.setAttribute( "onclick", "javascript: again(this);" );
but none of them works.
notice that I need this
as the parameter to send into the function.
And I need a javascript
solution not JQuery
What am I doing wrong?