0

I have the following Javascript functions. Yes() and No(). I want to add another function Ex(), but it won't let me? Yes() and No() just stop working when I add it, no matter where it's added in the code. Can somebody please explain what's wrong? Thanks.

function no() {
    location.reload();
}
function yes() {
    document.getElementById('ex').innerHTML = "<button onclick="ex()">Test</button>;
}
function ex() {
    alert('test');

}
1
  • 1
    You should have this in the function yes "<button onclick=\"ex()\">Test</button>"; You were missing the last quotation mark and escaping the inner ones. Commented Dec 24, 2016 at 13:21

2 Answers 2

1

escape you " in the string.

function no() {
    location.reload();
}
function yes() {
    document.getElementById('ex').innerHTML = "<button onclick=\"ex()\">Test</button>";
}
function ex() {
    alert('test');

}
yes();
<div id="ex"></div>

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

Comments

1

Alternatively you can change the outer quote with single quote for avoid escaping if you don't like one and keep the inner quote with double quote instead.

'<button onclick="ex()">Test</button>'

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.