Home >Web Front-end >JS Tutorial >How to get form elements in JavaScript

How to get form elements in JavaScript

autoload
autoloadOriginal
2021-04-07 15:36:592723browse

How to get form elements in JavaScript

Form in HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="" name="hello" id="login">
              <input type="text" placeholder="demo@email.com"
              <button>提交</button>
    </form>
</body>
</html>

Utilizedocument.formsGet all forms of the current page

  • ##document.forms[0]Get the first form of the current page A form

  • document.forms['hello']Get the form through the name attribute of the form

  • document.forms['login']Get the form through the id attribute of the form

    ##
        <script>
     //form
              console.log(document.forms);
     //  <form action="" name="hello" id="login">
              console.log(document.forms[0]);
              console.log(document.forms[&#39;hello&#39;]);
              console.log(document.forms[&#39;login&#39;]);
               
              console.log(document.forms.item(0));
              console.log(document.forms.item("hello"));
              console.log(document.forms.item("login"));
      //推荐 id
              console.log(document.forms.namedItem("login"));
              console.log(document.forms.hello);
              console.log(document.forms.login);
    
        </script>
  • Recommended: "
2021 js interview questions and answers (Large summary)

The above is the detailed content of How to get form elements in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn