19

I'm creating a button dynamically using JavaScript and at the same time assigning attributes such as 'ID', 'type' etc and also 'onclick' in order to trigger a function.

All works fine apart from the assignment of the 'onclick'. When clicked, the button is not triggering the function as it is supposed to. the function I'm trying to run is 'navigate(-1)' as seen below.

Where am I going wrong?

Here's my code:

function loadNavigation() {
  var backButton;
  backButton = document.createElement('input');
  backButton.ID = 'backButton';
  backButton.type = 'button';
  backButton.value='Back';
  backButton.onclick = 'navigate(-1)';
  document.body.appendChild(backButton);
}
3
  • 6
    An old question but this need to be said : you don't assign onclick anymore ;) use button.addEventListener('click', function) instead. Commented May 26, 2016 at 13:43
  • @AxelH What's the reason for using addEventListener over onclick? Commented May 31, 2016 at 14:51
  • 6
    Onclick is an old attribute to link a function to an event. AddEventListener use a collection to store the functions for each event, meaning that you can add as many function as you want for one event. This can prevent unexpected behavior if you have some script setting functionnalities that could override an other functionnalities. Onclick is useful for quick debug code but in production, I would not use. Commented Jun 1, 2016 at 6:48

7 Answers 7

33

As the other said you should assign a function.

Just wanted to point out that in this case you want to pass a value so you need to assign an anonymous function (or a named function defined inline) like

button.onclick = function() {otherfunction(parameter)};

If the function you want to assign does NOT require a parameter you can use it directly

button.onclick = otherfunction;

Note that there is no parenthesis in this case

button.onclick = otherfunction(); // this doesn't work

won't work as it will call otherfunction as soon as it is parsed

Sign up to request clarification or add additional context in comments.

Comments

4

you are assigning text to the onclick, try assigning a function.

backButton.onclick = function(){navigate(-1);};

Comments

4

In case this question is passed as a dupe, here is how to do it in current browsers

ES6

backButton.addEventListener("click",() => history.back());

Older but newer than onclick

backButton.addEventListener("click",function() { history.back() });

1 Comment

This answer works better in 2024 than the accepted answer
3

You have to assign a function, not a string.

backButton.onclick = function wastefulDuplicationOfBackButton () {
    navigate(-1);
}

2 Comments

@jessegavin, well, in IE you end up with two function objects due a serious bug on the JScript implementation of Named Function Expressions, not a such good thing :(...
Goes to show that you can do this for years and still learn new things … mostly new problems with IE, but new things nonetheless!
1

Use a function instead of a string. For example,

backButton.onclick = function () { navigate(-1); };

Comments

1

You should assign a function, not a string:

//...
backButton.onclick = function () {
  navigate(-1);
};
//...

Comments

0
backButton.onclick = function() { navigate(-1); }

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.