0

Javacript + html.

I have created an input of type "email" inside a form and a button. The user insert his mail to the input box, click the button and the input information will be saved to a cookie.

I have found that if the id name and onclick name on the button are similar it wouldn't work, I would like to understand why is that?

html:

<head>
    <title>Document</title>
    <link rel="stylesheet" href="main.css">
    <script src="main.js"></script>
</head>

<body>
    <form>
        <input id="input" type="email">
        <button id="saveMe" onclick="saveMe()">Save</button>
    </form>
</body>

</html>

JS:

function saveMe() {
    const input = document.getElementById("input")
    document.cookie = "Email=" + input.value
}
  • As per the duplicate, the scope of `onclick` **attributes** is stupid. It searches for `theFormElement.saveMe` before it tries `window.saveMe`. Don't use `on...` attributes. – Quentin Mar 04 '22 at 09:28

0 Answers0