How to update the onclick event of the element using pure javascript?












1















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...










share|improve this question




















  • 1





    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
















1















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...










share|improve this question




















  • 1





    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














1












1








1








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...










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 6:59









Foo

1




1










asked Nov 23 '18 at 6:49









my 0940my 0940

61




61








  • 1





    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














  • 1





    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








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












2 Answers
2






active

oldest

votes


















1














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">








share|improve this answer
























  • 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





















-1














Javascript :



document.getElementById("btn").attribute("onclick","javascript:func-y()");


Jquery :



$("#btn").attr("onclick","javascript:func-y()");





share|improve this answer



















  • 1





    close on first one but not quite right on method name

    – charlietfl
    Nov 23 '18 at 6:55











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
});


}
});














draft saved

draft discarded


















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









1














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">








share|improve this answer
























  • 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


















1














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">








share|improve this answer
























  • 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
















1












1








1







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">








share|improve this answer













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">






share|improve this answer












share|improve this answer



share|improve this answer










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





















  • 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















-1














Javascript :



document.getElementById("btn").attribute("onclick","javascript:func-y()");


Jquery :



$("#btn").attr("onclick","javascript:func-y()");





share|improve this answer



















  • 1





    close on first one but not quite right on method name

    – charlietfl
    Nov 23 '18 at 6:55
















-1














Javascript :



document.getElementById("btn").attribute("onclick","javascript:func-y()");


Jquery :



$("#btn").attr("onclick","javascript:func-y()");





share|improve this answer



















  • 1





    close on first one but not quite right on method name

    – charlietfl
    Nov 23 '18 at 6:55














-1












-1








-1







Javascript :



document.getElementById("btn").attribute("onclick","javascript:func-y()");


Jquery :



$("#btn").attr("onclick","javascript:func-y()");





share|improve this answer













Javascript :



document.getElementById("btn").attribute("onclick","javascript:func-y()");


Jquery :



$("#btn").attr("onclick","javascript:func-y()");






share|improve this answer












share|improve this answer



share|improve this answer










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














  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen