Willard
Willard

Reputation: 89

window.open popup textfield on focus when popping up

i have a page that opens a pop up

<script>
var popup = window.open('forms/trucksform.php');
popup.moveTo(0,0);
popup.resizeTo(318,340);
popup.focus();
popup.moveBy(500,200);
</script>

how can i focus on the textfield inside this popup

   <div class="mainform">
        <form name="addtrucks" id="addtrucks" method="POST" action="<?php echo  $editFormAction; ?>">
            <label>Truck Plate Number:</label>
            <input type="text" name="truck_plate_no" id="truck_plate_no" />
            <br/>
            <input name="button" type="submit" id="button" onclick="MM_validateForm('truck_plate_no','','R');return document.MM_returnValue"/>
            <input type="hidden" name="MM_insert" value="add" />
        </form>
    </div>

Upvotes: 0

Views: 2557

Answers (2)

Alexander
Alexander

Reputation: 809

You can use 2 ways: From the parent page you can get access to popup window:

<html>
<body>
<script>
var popup = window.open('trucksform.php');
popup.onload = function() {
    popup.document.getElementById("truck_plate_no").focus();
};
popup.moveTo(0,0);
popup.resizeTo(318,340);
popup.focus();
popup.moveBy(500,200);

</script>
</body>
</html>

Or add initial state to popup (this way better, because you will have reference to element on same page):

 <div class="mainform">
        <form name="addtrucks" id="addtrucks" method="POST" action="<?php echo  $editFormAction; ?>">
            <label>Truck Plate Number:</label>
            <input type="text" name="truck_plate_no" id="truck_plate_no" />
            <br/>
            <input name="button" type="submit" id="button" onclick="MM_validateForm('truck_plate_no','','R');return document.MM_returnValue"/>
            <input type="hidden" name="MM_insert" value="add" />
        </form>
    </div>
<script>
   document.getElementById("truck_plate_no").focus();   
</script>

Update for Chrome: It look as manipulation with popup window is blocking some UI functions. You can add to text input attribute autofocus="autofocus" in popup. To remove manipulation with popup window use parameters for open:

<html>
<body>
<script>
var popup = window.open('trucksform.php', 'myWindowName', 'left=200,top=500,width=318,height=340');
</script>
</body>
</html>

Upvotes: 1

Sridhar R
Sridhar R

Reputation: 20418

Try with

$("#truck_plate_no").focus();

Upvotes: 0

Related Questions