Set field on focus when loading page

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
riverman
User
Posts: 158
Location: Stockholm/Sweden

Set field on focus when loading page

Post by riverman »

I try to make a field in focus when page is loaded. I found two ways; in pure JavaScript and through jquery. But I don't get to work!

Two questions:
1) What is the correct name of the field id (in add-page)? x_firstname, q_x_firstname, sv_x_firstname or as_x_firstname?

Table: guests
Field: firstname

2) Where to put the Jquery or JavaScript?

I mange to narrow down the problem to:

Client Script -> Table_Specific -> Add/Copy Page -> Client Script

$(document).ready(function(){
$(this).find('#x_fieldname').focus();
});

Above works when NOT using lookup table! But as soon as I enable lookup table nothing happens! Is there an conflict or should I point to another id?


Webmaster
User
Posts: 9427

Post by Webmaster »

riverman wrote:
1) What is the correct name of the field id (in add-page)? x_firstname, q_x_firstname, sv_x_firstname or as_x_firstname?

You are probably using AutoSuggest. If so, the input field is not x_fieldname, right click the input in your browser and inspect the elements to find the correct id.


scs
User
Posts: 694

Post by scs »

Client Script -> Table_Specific -> Add/Copy Page -> Client Script

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


andyrav
User
Posts: 641

Post by andyrav »

Hi
I can set the focus, but how to i get the cursor to default to this box so i can just start typing in?

<input id="sv_x_contractRef" class="form-control tt-input" type="text" title="" data-toggle="tooltip" size="30" value="" name="sv_x_contractRef" data-original-title="Wiil Auto Fill. If Contract Ref not found Click Add" autocomplete="off" spellcheck="false" dir="auto" style="position: relative; vertical-align: top; background-color: transparent;">


riverman
User
Posts: 158
Location: Stockholm/Sweden

Post by riverman »

When not using AutoSuggest I both understand the code when inspecting and can make it work. But when activating AutoLookup the code is getting more complex f.ex more then one input field per field with ids:

sv_x_firstname, x_firstname, q_x_firstname and s_x_firstname

I have then tried with this code, but changed id to above:

$(document).ready(function(){
$('#sv_x_firstname').focus();
});

Also I did tests with this code:

$(document).ready(function(){
$(this).find('#x_fieldname').focus();
});

I regenerate the table and Other files, when loading page I reload with ctrl+F5...

I probably made some stupid mistake becouse I'm new to JavaScript/JQuery.

@scs Of course I have tried different solution but with no success. Thats way I ask for help... (So your answer was NOT an answer to my question)


Webmaster
User
Posts: 9427

Post by Webmaster »

riverman wrote:
$(document).ready(function(){
$('#sv_x_firstname').focus();
});

The approach is fine, but you'd better add a timer (make sure the AutoSuggest is set up), e.g. if the field name is "firstname",

$.later(200, null, function() {
$("#sv_x_firstname").focus();
});

If you use Modal dialog for Add page, you should use:

$("#ewModalDialog #sv_x_firstname").focus();


papirri3
User
Posts: 72

Post by papirri3 »

Thank you.


sangnandar
User
Posts: 980

Post by sangnandar »

How to do this with Modal-Lookup (v2017) ?
This is the input box,
<input type="text" name="sv" class="form-control" placeholder="Search">
Tried,
$('input[name="sv"]').focus(); // didn't work

Thanks.


sangnandar
User
Posts: 980

Post by sangnandar »

Never mind.
Got it,
$(document).ready(function() {
$('#ewModalLookupDialog').on("shown.bs.modal", function() {
$(this).find(".form-control:first").focus();
});
});


Post Reply