1

I would like to access the html input tag which is present inside a table inside a form as shown below. I have used the function below to access the element but I'm getting undefined in the output:

  function checkForm()
        {
            var  table=document.getElementById('register').getElementsByTagName('myTab');


            document.writeln(table);

        }

The above function returns undefined. Why, and how can it be fixed to return the input that I need?

        <!DOCTYPE HTML>
        <HEAD>
        <META CHARSET = "utf-8">
        <TITLE>Register</TITLE>
        <link rel="stylesheet" type="text/css" href="reglog.css">
        </HEAD>

        <BODY>
        <h1 align=center>Register with our portal</h1>

        <FORM id="register" onsubmit="return checkForm();" METHOD="POST">

        <table name="myTab" border = 0 align=center>

        <tr >
        <td> Username </td>
        <td> </td>
        <td><input name="username" class=textbox type = text required></td>
        </tr>


        <tr >
        <td> Password </td>
        <td> </td>
        <td><input name="passwd" class=textbox type = password required></td>
        </tr>


        <tr >
        <td> Confirm Password </td>
        <td> </td>
        <td><input name="c_password" class=textbox type = password required></td>
        </tr>



        <tr>
        <td> </td>
        <td> </td>
        <td> <input class=button type = submit value = Register> </td>
        </tr>


        </table>

        </FORM>
        </BODY>
        </HTML>
2
  • ...ByTagName <- it should be quite clear from the name that you're looking for elements that look like <myTab></myTab>. If you want to look for something with a name, it would be getElementsByName, and the "s" indicates that it returns more than one element, i.e. a nodeList. Commented Jul 24, 2017 at 13:43
  • Also, stop writing code like it was 1995. Remove the uppercase tagnames, and don't use document.writeln or inline javascript, make sure your attributes are quoted, and validate your HTML. Commented Jul 24, 2017 at 13:45

3 Answers 3

1

Javascript's getElementsByTagName function works, as it's name says, with html tag names (div, table, form, etc). In your case would be getElementsByTagName('table'). Instead you could use getElementsByName.

Sign up to request clarification or add additional context in comments.

Comments

1

To get the table element with attribute myTab from the element with id register, you can chain a querySelectorAll() method to a getElementById() :

table = document.getElementById('register').querySelectorAll('table[name=myTab]')[0];
console.log(table)
<h1 align=center>Register with our portal</h1>

<FORM id="register" onsubmit="return checkForm();" METHOD="POST">

  <table name="myTab" border=0 align=center>
    <tr>
      <td> Username </td>
      <td> </td>
      <td><input name="username" class=textbox type=t ext required></td>
    </tr>
    <tr>
      <td> Password </td>
      <td> </td>
      <td><input name="passwd" class=textbox type=p assword required></td>
    </tr>
    <tr>
      <td> Confirm Password </td>
      <td> </td>
      <td><input name="c_password" class=textbox type=p assword required></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td> <input class=button type=s ubmit value=R egister> </td>
    </tr>
  </table>
</FORM>

Note that getElementsByTagName() get the elements from their name, not from their name attribute.

Comments

0

There are various ways to access form elements. In your code you can access form elements using

var x = document.getElementById("register").elements;

This will give you all the elements in your form. However if you want to access first element you can use something like below code

 document.getElementById("register").elements[0].value // 0 is the index as per all the elements present in your form 'register'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.