Page 1 of 1

Set field on focus when loading page

Posted: Wed Aug 24, 2016 9:00 pm
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?


Re: Set field on focus when loading page (v12)

Posted: Thu Aug 25, 2016 4:34 pm
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.


Re: Set field on focus when loading page (v12)

Posted: Thu Aug 25, 2016 4:59 pm
by scs

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

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


Re: Set field on focus when loading page (v12)

Posted: Thu Aug 25, 2016 5:32 pm
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;">


Re: Set field on focus when loading page (v12)

Posted: Thu Aug 25, 2016 8:42 pm
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)


Re: Set field on focus when loading page

Posted: Fri Jun 16, 2017 3:38 pm
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();


Re: Set field on focus when loading page

Posted: Sun Jun 25, 2017 4:35 pm
by papirri3

Thank you.


Re: Set field on focus when loading page

Posted: Mon Sep 25, 2017 11:11 pm
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.


Re: Set field on focus when loading page

Posted: Tue Sep 26, 2017 3:13 am
by sangnandar

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