view raw
Daniel Choi Daniel Choi - 1 year ago 38
HTML Question

onClick event doesn't work inside form tag

<!DOCTYPE html>
function removeP2() {
var parent = document.getElementById("section");
var child = document.getElementById("p2");
<button onclick="removeP2();">Remove</button>
<section id="section">
<p id="p1">Paragraph One.</p>
<p id="p2">Paragraph Two.</p>

When I click the submit button, the function executes as usual, but after the function executes, the page reloads itself (removed P2 for a second).

I found a solution which is to delete the "Form" inside the "nav" tag, and it works fine. Just want to ask what is the problem cause that, and if I need to have the "Form" tag inside the "nav" tag, which part I need to fix?


Use the code below. It won't submit the form.

<button type="button">My Button</button>