0

I have a button

<button id="buttonOne" onclick="pressOne()">Press</button>

i was wondering if it was possible using javascript to change the

onclick="pressOne()"

to

onclick="pressTwo()"

so the button would be

<button id="buttonOne" onclick="pressTwo()">Press</button>

Thanks!

2
  • It would be better to contain the condition which determines which logic to run within a single function. What governs which function should be executed on click? Commented May 7, 2015 at 8:50
  • hmmm yeah its possible.... stackoverflow.com/questions/13229208/… Commented May 7, 2015 at 8:50

3 Answers 3

1

You can change it using this:

$("#buttonOne").attr("onclick","pressTwo()");

For example:

function pressOne() {
    $("#buttonOne").attr("onclick","pressTwo()");
}
Sign up to request clarification or add additional context in comments.

Comments

0

Write this:

$("#buttonOne").attr("onclick","pressTwo()");

FIDDLE - Inspect element to see.

Comments

0

You can do that like this :

document.getElementById("buttonOne").setAttribute("onclick","pressTwo()");

jsFiddle


But it would be better to add an other function which handle clicks :

<button id="buttonOne" onclick="onPress()">Press</button>

With the following javascript :

var pressOne = function() {
  ...
}

var pressTwo = function() {
  ...
}

function onPress() {
  if(..) {
    pressOne();
  } else {
    pressTwo();
  }
}

jsFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.