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

How to get form elements in JavaScript

autoload
Release: 2021-04-07 15:36:59
Original
2653 people have browsed it

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>
Copy after login

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>
    Copy after login
    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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template