1

I have a function with input var is name of Form and ID of input. Now I want to get the value of password input. The problem is I have some input with same ID but in different form. So I want a flexible way to get its value when I know its form.

I have tried var x = Form_Name.ID_Name.value;

but it doesn't work. So please tell me how to get the value of password input by a flexible way. Thanks so much. Below is my code:

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="lolkittens" />
    <script charset="utf-8" type="text/javascript">
        function check(Form_Name, ID_Name)
        {
            alert(document.getElementById(ID_Name).value);
            alert(Form_Name);
            var x = Form_Name.ID_Name.value;
            alert(x);
        }
    </script>
    <title>Untitled 1</title>
</head>

<body>

    <div id="main" style="width: 300px; height: 200px;">
        <form name="MainForm" id="MainForm">
            <input id="ID" name="ID" type="text" size="20"/>
            <input id="Pass" name="Pass" type="password" size="20"/>
        </form>
        <button value="click Me" onclick="check('MainForm','ID')"> Click Me </button>
    </div>
</body>
</html>
6
  • 6
    To get password value, you can use document.getElementById('Pass').value Commented Dec 23, 2015 at 8:49
  • 1
    Use regular DOM methods like document.getElementById(), document.getElementsByTagName(), etc. Exposing every item as a global variable is something you can expect in Internet Explorer 4 but not in modern browsers. Commented Dec 23, 2015 at 8:51
  • @ÁlvaroGonzález where do you see global variables ? Form_Name is a string .. also console.log(Pass.value); work in latest chrome Commented Dec 23, 2015 at 9:00
  • @Hacketo I thought the intention of Form_Name.ID_Name.value might be that even though the call passes a string. Commented Dec 23, 2015 at 9:31
  • @ÁlvaroGonzález I guess you're right about the intention, but this way work in modern browsers (at least on my chrome and firefox). this work console.log(window[Form_Name][ID_Name].value); Commented Dec 23, 2015 at 9:35

2 Answers 2

4

I have created another button for you to get password.

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="lolkittens" />
    <script charset="utf-8" type="text/javascript">
        function check(Form_Name, ID_Name)
        {
            alert(document.getElementById(ID_Name).value);
        }
    </script>
    <title>Untitled 1</title>
</head>

<body>

    <div id="main" style="width: 300px; height: 200px;">
        <form name="MainForm" id="MainForm">
            <input id="ID" name="ID" type="text" size="20"/>
            <input id="Pass" name="Pass" type="password" size="20"/>
        </form>
        <button value="click Me" onclick="check('MainForm','ID')"> Click Me for ID </button>
        <button value="click Me" onclick="check('MainForm','Pass')"> Click Me for Password</button>
    </div>
</body>
</html>

Optimized way: You can achieve it using the same button as well, by passing an array of ID's.

Let me know, I can help more on this.

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

4 Comments

it would help OP to remove the useless code, and explain what is wrong with Form_Name.ID_Name.value
Yes rightly pointed out. editing the code to remove useless code.
@bozzmob: Thanks so much. I fixed it like this and it work: var x = window[Form_Name][ID_Name].value; alert(x); The problem is I have some input with same ID but in different form. So I want a flexible way to get its value when I know its form.
@bozzmob: Is ` <meta name="author" content="lolkittens" />` useful code?
2

Access username and password using it's id.

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="lolkittens" />
    <script charset="utf-8" type="text/javascript">
        function check()
        {
            var username = document.getElementById("ID").value;
            var password = document.getElementById("Pass").value;
            alert("Username: "+username+"  Password: "+password);
        }
    </script>
    <title>Untitled 1</title>
</head>

<body>

    <div id="main" style="width: 300px; height: 200px;">
        <form name="MainForm" id="MainForm">
            <input id="ID" name="ID" type="text" size="20"/>
            <input id="Pass" name="Pass" type="password" size="20"/>
        </form>
        <button value="click Me" onclick="check()"> Click Me </button>
    </div>
</body>
</html>

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.