0

I'm trying to add an onclick event with JavaScript.

$.getJSON(api , function(data) {
    //First Option found (not working)
    //document.getElementById("today_button").SetAttrribute("onclick", "window.location.replace('" + data[1] + "')");

    //Second Option found (not working either)
    document.getElementById('today_button').onclick = "window.location.replace('" + data[1] + "');";
});

The Variables exist, the Event is fired, the data transmitted are correct and everything except this works just fine (like altering the disabled state of exactly this button).

I hope you can help me

3 Answers 3

3

You need to assign a function to onclick, not a string:

document.getElementById('today_button').onclick = function(event) {
   window.location.replace(data[1]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the answer
0

Why are you assigning a string as a click handler? Just use a function.

document.getElementById('today_button').onclick = function () {
    location.replace(data[1]);
};

Comments

0

The problem is you assigned string to onclick, but it needs function.

document.getElementById('today_button').onclick = function() {
    window.location.replace(data[1]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.