I Love PHPMaker

... because it gets even more powerful and flexible!!

  • About
  • Terms and Conditions
  • Membership Options
  • Sitemap
  • Downloads
    • PHPMaker Extensions Download
    • PHPMaker Projects Download
    • PHP Report Maker Extensions Download
I Love PHPMaker » Tips and Trick » How to Set Focus on Next Input Field Using jQuery When Users Press Enter in Websites that Generated With PHPMaker 8.0.3
How to Add the Password Strength Meter and Password Validation Features on Registration Page in Websites that Generated With PHPMaker 8.0.3
How to Make the Different Filter for List and Add/Edit Page in Websites that Created With PHPMaker 8.0.3

March 1, 2012

How to Set Focus on Next Input Field Using jQuery When Users Press Enter in Websites that Generated With PHPMaker 8.0.3

I believe that almost all web application users want that when they fill out the application data into a form, then they can use the Enter key to move the cursor from one input field to the next input field quickly. As we know that by default, to switch from one Input Field to the next Input Field on a form, users must use the Tab key. Unfortunately, PHPMaker has not handled the ability to use Enter key in order to move the cursor to the next Input Field.

To overcome this issue in a web application that generated by PHPMaker, I have found a way how this can be solved by using jQuery. We will use Enter key to move the cursor from one Input Field to another. In other words, each time user press the Enter key, then the next Input Field will automatically get focus. It also will handle if the last element in that form is the button, then when user press Enter, it will press the button and submit the form.

Unlike a lot of source code we found on the Internet that only discuss how to move the cursor from the same Input Fields (usually they only use TextBoxes for such case), then the following trick will handle the different Input Field types (TextBox, ComboBox/Select Option, and Option Button/Radio Button) that their order may not be always sequential. For example: You may have the order of the Input Fields in your form as following: "TextBox -> ComboBox -> RadioButton -> ComboBox -> TextBox" or "ComboBox -> ComboBox -> TextBox -> RadioButton -> ComboBox -> TextBox", etc. Many examples we found on the Internet have not handled those possibilities. Well, don't worry anymore, as I have managed all of those possibilities, too!

Important to know, that I only applied this trick to the three types of Input Fields. They are: Input (TextBox), Select (ComboBox), and Radio (Option Button). I did not apply this trick for TextArea, since users often require Enter key to add a new row in the data that they are typing. Does it make sense to you?

In addition, the following trick also will show you how to cope if the Input Fields that have been generated by PHPMaker is hidden or disabled. We also have to handle this situation, right? Why? Because we have to avoid undesirable condition (usually the cursor will not move as it should if the next Input Field is hidden or disabled).

Another advantages of this trick, it does not interfere with the function of the Enter key to select the record on the list that appears when you are using the Auto Fill feature for the TextBox field. In addition, after you select the data by pressing the Enter key to fill out the record into the TextBox which has the Auto Fill feature before, then the cursor will automatically move to the next Input field after the desired record had been chosen. It also will handle if the next element is the submit button.

This trick also includes the feature that will automatically set focus on any type of Input Field which was first found in the newly displayed page. The impact is, if you display a language selector in the top (header) of your web application, then the cursor will automatically focus on it first, because it is the first element / that appears on the top of the page, right? But don't worry, I will also show you how to pass the language selector, if the next element type after it is still ComboBox.

All of those tricks will be placed into the Client Scripts section that belongs to either Add/Copy pages or Edit pages of your PHPMaker. In other words, no need to modify the generated scripts.

Again! Your life and your users' life become easier with I Love PHP Maker. 🙂

[hidepost]

  1. Open your PHPMaker project (.pmp) file using PHPMaker application.
  2. Click on Database icon on the toolbar, then click one of your desired table, and then click on the Server Events/Client Scripts tab. Go to the Client Scripts -> Table-Specific -> Add/Copy Page -> Client Script, and insert this following code:

    $(document).ready(function() {

    var elem = $('input:visible', this).get(0);
    var textarea = $('textarea:visible', this).get(0);
    if (textarea && elem) {
    if (textarea.offsetTop < elem.offsetTop) { elem = textarea; } } var select = $('select:visible', this).get(0); // <-- replace 0 with 1 if you want to skip focus on the language selector! if (select && elem) { if (select.offsetTop < elem.offsetTop) { elem = select; } } if (elem) { elem.focus(); } $('input').keydown( function (event) { //event==Keyevent if(event.which == 13) { var inputs = $(this).closest('form').find(':input:visible:enabled'); var idx = inputs.index(this); if (idx == inputs.length - 1) { // handling submit button! } else { inputs.eq( inputs.index(this)+ 1 ).focus(); event.preventDefault(); //Disable standard Enterkey action } } }); $('select').keydown( function (event) { //event==Keyevent if(event.which == 13) { var inputs = $(this).closest('form').find(':input:visible:enabled'); var idx = inputs.index(this); if (idx == inputs.length - 1) { // handling submit button! } else { inputs.eq( inputs.index(this)+ 1 ).focus(); event.preventDefault(); //Disable standard Enterkey action } } }); $('radio').keydown( function (event) { //event==Keyevent if(event.which == 13) { var inputs = $(this).closest('form').find(':input:visible:enabled'); var idx = inputs.index(this); if (idx == inputs.length - 1) { // handling submit button! } else { inputs.eq( inputs.index(this)+ 1 ).focus(); event.preventDefault(); //Disable standard Enterkey action } } }); }); [/code]

  3. Insert the following code for the Edit Page -> Client Script that located at the same level with the Add/Copy Page:

    // Enter to move to next Input Field
    $(document).ready(function() {
    var elem = $('input:visible', this).get(0);
    var textarea = $('textarea:visible', this).get(0);
    if (textarea && elem) {
    if (textarea.offsetTop < elem.offsetTop) { elem = textarea; } } var select = $('select:visible', this).get(0); // <-- replace 0 with 1 if you want to skip focus on the language selector! if (select && elem) { if (select.offsetTop < elem.offsetTop) { elem = select; } } if (elem) { elem.focus(); elem.select(); } $('input').keydown( function (event) { //event==Keyevent if(event.which == 13) { var inputs = $(this).closest('form').find(':input:visible:enabled'); var idx = inputs.index(this); if (idx == inputs.length - 1) { // handling submit button! } else { inputs.eq( inputs.index(this)+ 1 ).focus(); inputs.eq( inputs.index(this)+ 1 ).select(); event.preventDefault(); //Disable standard Enterkey action } } }); $('select').keydown( function (event) { //event==Keyevent if(event.which == 13) { var inputs = $(this).closest('form').find(':input:visible:enabled'); var idx = inputs.index(this); if (idx == inputs.length - 1) { // handling submit button! } else { inputs.eq( inputs.index(this)+ 1 ).focus(); inputs.eq( inputs.index(this)+ 1 ).select(); event.preventDefault(); //Disable standard Enterkey action } } }); $('radio').keydown( function (event) { //event==Keyevent if(event.which == 13) { var inputs = $(this).closest('form').find(':input:visible:enabled'); var idx = inputs.index(this); if (idx == inputs.length - 1) { // handling submit button! } else { inputs.eq( inputs.index(this)+ 1 ).focus(); inputs.eq( inputs.index(this)+ 1 ).select(); event.preventDefault(); //Disable standard Enterkey action } } }); }); [/code] The difference between the code for Add and Edit is that we add the calling of select() below the calling of focus() in order to highlight all characters in the textbox for Edit mode.

  4. Repeat the step number two and three above for another tables you desire.
  5. Please note that if you want to skip focus on the language selector (if your web application is running the multi language feature), then change this code:

    var select = $('select:visible', this).get(0); // <-- replace 0 with 1 if you want to skip focus on the language selector! [/code] with the following one: [code lang="php"] var select = $('select:visible', this).get(1); // <-- 1 = skip focus on the language selector! [/code]

  6. Finally, don't forget to save the changes, and re-generate your script files as usual using PHPMaker.

[/hidepost]

Article by Masino Sinaga / Tips and Trick / autofocus, Enter, focus, Input Field, jQuery, PHPMaker 8.0.3, setfocus 5 Comments

How to Add the Password Strength Meter and Password Validation Features on Registration Page in Websites that Generated With PHPMaker 8.0.3
How to Make the Different Filter for List and Add/Edit Page in Websites that Created With PHPMaker 8.0.3

Comments

  1. Juan Manuel Garcia says

    July 4, 2018 at 11:14 am

    Hello Masino,
    I could never make the second or any other field than the first one take the focus when loading the Add or Edit page.
    I have too many doubts but you do not answer or when you do, you tell me to look at your example and it does not contain what I need.
    I’m going to cancel my membership and maybe come back later. What I do tell you is that I’m not very happy with your page.
    Thanks anyway.
    Greetings.

    Log in to Reply
    • Masino Sinaga says

      July 12, 2018 at 5:54 pm

      Apologized for my delay response. You should not have any doubts after you try it first, and check whether any error messages displayed or not. I cannot help you if I did not know the cause of your problem, for example, from the error message that returned by the web app via browser.

      Log in to Reply
  2. Nelson Hernando Montoya says

    October 9, 2018 at 10:17 pm

    Masino

    good morning

    Attentive to the new things that will come with version 2019

    as always surprising

    Thank you

    Log in to Reply
    • Masino Sinaga says

      November 19, 2018 at 8:38 am

      Hi Nelson. PHPMaker 2019 had been released, and yes, I have been working on it to create the Masino Extensions for PHPMaker 2019. Please be patient, as it need some more time to implement Masino Extensions for v2019. I will release Masino Extensions for v2019 on January 2019.

      Log in to Reply

Trackbacks

  1. How to Set Focus on Next Input Field Using jQuery When Users Press Enter in Websites that Generated by PHPMaker 9.0.2 — I Love PHPMaker says:
    June 11, 2012 at 9:22 am

    […] How to Set Focus on Next Input Field Using jQuery When Users Press Enter in Websites that Generated by PHPMaker 9.0.2 June 11, 2012 By Masino Sinaga Leave a Comment This following trick will show you how to move cursor by using Enter key from one input field to the next input field on a form in websites that generated by PHPMaker 9. Instead of using Tab key, then we will use Enter key in order to set focus the next input field on a form while users are inserting or editing record. You can read the complete explanation from this article. […]

    Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • A New PHPMaker 2025 Project File Is Released
  • PHPMaker 2025 Demo Project File Is Released
  • Masino Extensions for PHPMaker 2025 Is Released!
  • A New PHPMaker 2024 Project File Is Released
  • PHPMaker 2024 Demo Project File Is Released
  • Masino Extensions for PHPMaker 2024 Is Released!
  • PHPMakerProjects.com, For Those Who Need PHPMaker Project Sample
  • A New PHPMaker 2023 Project File Is Released
  • PHPMaker 2023 Demo Project File Is Released
  • Masino Extensions for PHPMaker 2023 Is Released!

Search

Recent Comments

  • Masino Sinaga on Masino Extensions for PHPMaker 2024 Is Released!
  • Masino Sinaga on A New PHPMaker 2024 Project File Is Released
  • Masino Sinaga on PHPMaker 2023 Demo Project File Is Released
  • Edward Babatunde on PHPMaker 2023 Demo Project File Is Released
  • Edward Babatunde on Masino Extensions for PHPMaker 2024 Is Released!

Demo Website

  • Demo of I Love PHPMaker 2025 (MasinoExtensions).
  • Stock Inventory Management for PHPMaker 2025.

Another Demo

The following template are not available in this site (must be purchased separately)

  • PHPMaker v2018 Horizontal Vertical Template.
  • PHPMaker v2017 Horizontal Vertical Template.

Demo Explanation

Stock Inventory Management is the good project for your reference, since it uses the real example in the real world. Many useful features you can use from this project, such as how to add the Thousand and Decimal separator character, and also how to calculate multiple row in Grid-Add when End-Users are entering data into the Grid-Add mode.

Categories

  • Customize Template (103)
  • General (4)
  • PHP Report Maker (17)
  • PHP Report Maker Extensions (2)
  • PHPMaker Extensions (84)
  • PHPMaker Projects (7)
  • Tips and Trick (72)

Articles based on version

  • PHPMaker 2025
  • PHPMaker 2024
  • PHPMaker 2023
  • PHPMaker 2022
  • PHPMaker 2021
  • PHPMaker 2020
  • PHPMaker 2019
  • PHPMaker 2018
  • PHPMaker 2017.0.7
  • PHPMaker 12.0.7
  • PHPMaker 11.0.6
  • PHPMaker 10.0.5
  • PHPMaker 9.2.0
  • PHPMaker 8.0.3
  • PHP Report Maker 12

(c) I Love PHPMaker 2010 - 2025 by Masino Sinaga | WordPress | Log in | Back to top