How to update the onclick event of the element using pure javascript?
how to change onclick="javascript:func-x()"
<input type="button" onclick="javascript:func-x()" value="MENU" id="btn">
to onclick="javascript:func-y()" with same id on javascript?
<input type="button" onclick="javascript:func-y()" value="MENU" id="btn">
I've tried:
var c = document.getElementById('btn'); //input#btn
// ...
But I don't know what to continue...
javascript onclick
add a comment |
how to change onclick="javascript:func-x()"
<input type="button" onclick="javascript:func-x()" value="MENU" id="btn">
to onclick="javascript:func-y()" with same id on javascript?
<input type="button" onclick="javascript:func-y()" value="MENU" id="btn">
I've tried:
var c = document.getElementById('btn'); //input#btn
// ...
But I don't know what to continue...
javascript onclick
1
ConsideraddEventListener
andremoveEventListener
instead, might make things easier to manage
– CertainPerformance
Nov 23 '18 at 6:56
id must be unique.
– Bhojendra Rauniyar
Nov 23 '18 at 6:57
add a comment |
how to change onclick="javascript:func-x()"
<input type="button" onclick="javascript:func-x()" value="MENU" id="btn">
to onclick="javascript:func-y()" with same id on javascript?
<input type="button" onclick="javascript:func-y()" value="MENU" id="btn">
I've tried:
var c = document.getElementById('btn'); //input#btn
// ...
But I don't know what to continue...
javascript onclick
how to change onclick="javascript:func-x()"
<input type="button" onclick="javascript:func-x()" value="MENU" id="btn">
to onclick="javascript:func-y()" with same id on javascript?
<input type="button" onclick="javascript:func-y()" value="MENU" id="btn">
I've tried:
var c = document.getElementById('btn'); //input#btn
// ...
But I don't know what to continue...
javascript onclick
javascript onclick
edited Nov 23 '18 at 6:59
Foo
1
1
asked Nov 23 '18 at 6:49
my 0940my 0940
61
61
1
ConsideraddEventListener
andremoveEventListener
instead, might make things easier to manage
– CertainPerformance
Nov 23 '18 at 6:56
id must be unique.
– Bhojendra Rauniyar
Nov 23 '18 at 6:57
add a comment |
1
ConsideraddEventListener
andremoveEventListener
instead, might make things easier to manage
– CertainPerformance
Nov 23 '18 at 6:56
id must be unique.
– Bhojendra Rauniyar
Nov 23 '18 at 6:57
1
1
Consider
addEventListener
and removeEventListener
instead, might make things easier to manage– CertainPerformance
Nov 23 '18 at 6:56
Consider
addEventListener
and removeEventListener
instead, might make things easier to manage– CertainPerformance
Nov 23 '18 at 6:56
id must be unique.
– Bhojendra Rauniyar
Nov 23 '18 at 6:57
id must be unique.
– Bhojendra Rauniyar
Nov 23 '18 at 6:57
add a comment |
2 Answers
2
active
oldest
votes
You can try to use setAttribute
method, like this:
function func_x(input) {
console.log('I'm func_x function');
input.setAttribute('onclick', 'javascript:func_y()');
}
function func_y() {
console.log('I'm func_y function');
}
<input type="button" onclick="javascript:func_x(this)" value="MENU" id="btn">
thanks! It worked!
– my 0940
Nov 24 '18 at 4:19
@my0940 You're welcome. You can mark this answer as an accept answer by clicking on the check icon near by the upvote icon
– Foo
Nov 24 '18 at 6:14
add a comment |
Javascript :
document.getElementById("btn").attribute("onclick","javascript:func-y()");
Jquery :
$("#btn").attr("onclick","javascript:func-y()");
1
close on first one but not quite right on method name
– charlietfl
Nov 23 '18 at 6:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441857%2fhow-to-update-the-onclick-event-of-the-element-using-pure-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try to use setAttribute
method, like this:
function func_x(input) {
console.log('I'm func_x function');
input.setAttribute('onclick', 'javascript:func_y()');
}
function func_y() {
console.log('I'm func_y function');
}
<input type="button" onclick="javascript:func_x(this)" value="MENU" id="btn">
thanks! It worked!
– my 0940
Nov 24 '18 at 4:19
@my0940 You're welcome. You can mark this answer as an accept answer by clicking on the check icon near by the upvote icon
– Foo
Nov 24 '18 at 6:14
add a comment |
You can try to use setAttribute
method, like this:
function func_x(input) {
console.log('I'm func_x function');
input.setAttribute('onclick', 'javascript:func_y()');
}
function func_y() {
console.log('I'm func_y function');
}
<input type="button" onclick="javascript:func_x(this)" value="MENU" id="btn">
thanks! It worked!
– my 0940
Nov 24 '18 at 4:19
@my0940 You're welcome. You can mark this answer as an accept answer by clicking on the check icon near by the upvote icon
– Foo
Nov 24 '18 at 6:14
add a comment |
You can try to use setAttribute
method, like this:
function func_x(input) {
console.log('I'm func_x function');
input.setAttribute('onclick', 'javascript:func_y()');
}
function func_y() {
console.log('I'm func_y function');
}
<input type="button" onclick="javascript:func_x(this)" value="MENU" id="btn">
You can try to use setAttribute
method, like this:
function func_x(input) {
console.log('I'm func_x function');
input.setAttribute('onclick', 'javascript:func_y()');
}
function func_y() {
console.log('I'm func_y function');
}
<input type="button" onclick="javascript:func_x(this)" value="MENU" id="btn">
function func_x(input) {
console.log('I'm func_x function');
input.setAttribute('onclick', 'javascript:func_y()');
}
function func_y() {
console.log('I'm func_y function');
}
<input type="button" onclick="javascript:func_x(this)" value="MENU" id="btn">
function func_x(input) {
console.log('I'm func_x function');
input.setAttribute('onclick', 'javascript:func_y()');
}
function func_y() {
console.log('I'm func_y function');
}
<input type="button" onclick="javascript:func_x(this)" value="MENU" id="btn">
answered Nov 23 '18 at 6:56
FooFoo
1
1
thanks! It worked!
– my 0940
Nov 24 '18 at 4:19
@my0940 You're welcome. You can mark this answer as an accept answer by clicking on the check icon near by the upvote icon
– Foo
Nov 24 '18 at 6:14
add a comment |
thanks! It worked!
– my 0940
Nov 24 '18 at 4:19
@my0940 You're welcome. You can mark this answer as an accept answer by clicking on the check icon near by the upvote icon
– Foo
Nov 24 '18 at 6:14
thanks! It worked!
– my 0940
Nov 24 '18 at 4:19
thanks! It worked!
– my 0940
Nov 24 '18 at 4:19
@my0940 You're welcome. You can mark this answer as an accept answer by clicking on the check icon near by the upvote icon
– Foo
Nov 24 '18 at 6:14
@my0940 You're welcome. You can mark this answer as an accept answer by clicking on the check icon near by the upvote icon
– Foo
Nov 24 '18 at 6:14
add a comment |
Javascript :
document.getElementById("btn").attribute("onclick","javascript:func-y()");
Jquery :
$("#btn").attr("onclick","javascript:func-y()");
1
close on first one but not quite right on method name
– charlietfl
Nov 23 '18 at 6:55
add a comment |
Javascript :
document.getElementById("btn").attribute("onclick","javascript:func-y()");
Jquery :
$("#btn").attr("onclick","javascript:func-y()");
1
close on first one but not quite right on method name
– charlietfl
Nov 23 '18 at 6:55
add a comment |
Javascript :
document.getElementById("btn").attribute("onclick","javascript:func-y()");
Jquery :
$("#btn").attr("onclick","javascript:func-y()");
Javascript :
document.getElementById("btn").attribute("onclick","javascript:func-y()");
Jquery :
$("#btn").attr("onclick","javascript:func-y()");
answered Nov 23 '18 at 6:54
dvl333dvl333
495
495
1
close on first one but not quite right on method name
– charlietfl
Nov 23 '18 at 6:55
add a comment |
1
close on first one but not quite right on method name
– charlietfl
Nov 23 '18 at 6:55
1
1
close on first one but not quite right on method name
– charlietfl
Nov 23 '18 at 6:55
close on first one but not quite right on method name
– charlietfl
Nov 23 '18 at 6:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441857%2fhow-to-update-the-onclick-event-of-the-element-using-pure-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Consider
addEventListener
andremoveEventListener
instead, might make things easier to manage– CertainPerformance
Nov 23 '18 at 6:56
id must be unique.
– Bhojendra Rauniyar
Nov 23 '18 at 6:57