1

How can I get the textbox which is in a form and that form is in an iframe from javascript? The simplified code is:

<!-- other code --> 
    <div id="Login_Panel_Div">

        <iframe id="popupLoginScreen" src="login.jsp" name="popupContent">

            <html xmlns="http://www.w3.org/1999/xhtml">

            <head>
            </head>

            <body>

                <form id="loginForm" method="post" name="loginForm">
                    <h2>Login to your account</h2>                      

                    <label>Username:</label>

                    <input id="username" type="text" name="j_username">

                    <label>Password:</label>

                    <input id="password" type="password" name="j_password">

                    <button id="submitLoginBtn" value="Login" name="submitLoginBtn" type="submit">Login</button>
                </form>

            </body>

            </html>

        </iframe>

    </div>
<!-- other code -->

If I want to get the input with id "txtbox" what should I do?

Thanks and Regards.

Tapas Bose
  • 25,780
  • 71
  • 202
  • 317
  • 1
    possible duplicate with http://stackoverflow.com/questions/1088544/javascript-get-element-from-within-an-iframe – xiao 啸 May 11 '11 at 10:18

1 Answers1

1

As xiao 啸 pointed me, the solution is:

var iframe = document.getElementById('popupLoginScreen');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var usernameTextBox = innerDoc.getElementById('username');
usernameTextBox.focus();

Requesting for thread close.

Thanks.

Community
  • 1
  • 1
Tapas Bose
  • 25,780
  • 71
  • 202
  • 317