html - Get data from a FORM in javascript -


i need data form . form :

<form action="" id="test">    <h2>data</h2>    <label for="">name</label>    <input type="text" name="name">    <label for="">address</label>    <input type="text" name="address">    <label for="">phone</label>    <input type="text" name="phone">    <input type="button" onclick="xd()" value="click here"> </form> 

and used javascript command :

 <script type="text/javascript">  function xd(){  //var x=document.forms["test"].getelementsbytagname("address") or  var x=document.forms["test"].getelementsbytagname("address").value  document.write(x);  }  </script> 

but it's not working. how easily.

you seem targeting element name attribute (address), not tagname attribute.

.getelementsbytagname finds elements in container have specific tagname...meaning tagname <a href="#">asdf</a> element "a".

i use:

<script type="text/javascript">     function xd(){         var inputs = document.forms["test"].getelementsbytagname("input");         (var = 0 ; < inputs.length; i++) {             if (inputs[i].name === "address") {                 // `inputs[i]` element name "address"             }         }     } </script> 

you have loop through results, , compare matched elements' name attribute against want..."address".

of course, option use getelementsbyname("address"), like:

var address = document.forms["test"].getelementsbyname("address"); if (address.length > 0) {     // `address[0]` element name "address" } 

and final option use queryselectorall('input[name="address"]'), like:

var address = document.forms["test"].queryselectorall('[name="address"]'); if (address.length > 0) {     // `address[0]` element name "address" } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -