Is there a way to use jQuery to focus the cursor on a specific field in a form?

In my form, the first field is a dropdown with options such as "student" and "employee". Below that, there are two text fields for "college name" and "company name". If a person selects "student", then the "college name" text box should be enabled and the "company name" text box should be disabled. If a person selects "employee", then the "company name" field should be enabled and the "college name" field should be disabled. The cursor focus should automatically move to a particular field based on the user's selection of the dropdown (for example, upon selecting "student", the "company" field should be disabled and the cursor focus should point to the "college" field, and vice versa). How can I achieve this?


    <div class="container">
      <div class="row main">
        <div class="panel-heading">
          <div class="panel-title text-center">
            <h1 class="title">Company Name</h1>
            <hr />
          </div>
        </div> 
        <div class="main-login main-center">
          <form class="form-horizontal" method="post" action="#">

            <div class="form-group">
              <label for="name" class="cols-sm-2 control-label">I am a</label>
              <div class="cols-sm-10">
                <div class="input-group">
                  <span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
                    <select name="cars">
                      <option value="volvo">Student</option>
                      <option value="saab">Employee</option>

                    </select>
                </div>
              </div>
            </div>

            <div class="form-group">
              <label for="email" class="cols-sm-2 control-label">College Name</label>
              <div class="cols-sm-10">
                <div class="input-group">
                  <span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
                  <input type="text" class="form-control" name="email" id="email"  placeholder="Enter your Email"/>
                </div>
              </div>
            </div>

            <div class="form-group">
              <label for="username" class="cols-sm-2 control-label">Company Name</label>
              <div class="cols-sm-10">
                <div class="input-group">
                  <span class="input-group-addon"><i class="fa fa-users fa" aria-hidden="true"></i></span>
                  <input type="text" class="form-control" name="username" id="username"  placeholder="Enter your Username"/>
                </div>
              </div>
            </div>

            <div class="form-group ">
              <button type="submit" class="btn btn-primary btn-lg btn-block login-button">Register</button>
            </div>
            <div class="login-register">
              <a href="index.php">Login</a>
            </div>
          </form>
        </div>
      </div>
    </div>

Answer №1

To solve the issue, apply the $(selector).focus() function along with an if-else statement based on the condition. This method should assist in resolving the problem effectively. The selector can be identified using a suitable class or id.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Find and retrieve all data attributes with JavaScript

What is the method to retrieve all data-attributes and store them in an array? <ul data-cars="ford"> <li data-model="mustang"></li> <li data-color="blue"></li> <li data-doors="5"></li> </ul> The resultin ...

Manipulating the DOM with AngularJS directives is not functioning as expected

I am facing an issue with integrating a code that needs to scroll slowly on the page using AngularJS 1.4. When I try to include this code in a directive using the link function(scope, element, attrs), it doesn't work properly. Interestingly, the code ...

Populate HTML dropdown menu with data from an external JSON file

I'm attempting to populate a HTML Dropdown menu with information from an external JSON file, which consists of the following { "Destinations": [ { "destinationName": "London", "destinationID": "lon" }, { "dest ...

An illustration of HTML frames

Is this method effective? <html> <body> <center> <frameset rows="50%,50%"> <frame src="images.html"> <frameset> <form action="abc.html"> <input type="submit" name="abc page" value="abc page"> </form> & ...

How can I delete already uploaded files before adding new ones in fileupload js?

I am facing an issue while using fileupload js to upload multiple files. The problem is that when I add files for the first time and then try to replace them with different files, all the files get uploaded instead of just the new ones. Can anyone guide me ...

Move the cursor within the text area upon clicking the button

When the "+header" button is clicked, I am looking to automatically position the insertion point inside the text area. Currently, after pressing the button, the text box displays information like address, date, time etc. but the cursor does not start insid ...

Updating the value on button click using Rails 5, Ajax, and jQuery

Whenever the user clicks on the "Add to Cart" button, I want to update the number in my navbar div to display how many products are currently in their cart. To achieve this, I have set the remote: true attribute on the button. The navbar contains a div w ...

What changes should I make to my save function in order to handle both saving and editing user information seamlessly?

My goal for the save function is to achieve two tasks: 1. Save user in table - Completed 2. Update user in table - In progress Save function snippet: var buildUser = function () { $('#save').click(function () { var newUser = {}; ...

Determine the overall sum of all items

Utilizing data fetched from my SQLite database, I'm utilizing *ngFor to display a comprehensive list of items with their respective names, prices, amounts, and totals. How can I calculate the grand total and display it at the bottom of the list? chec ...

Invoke a confirmation dialog with onBeginForm() function prior to initiating OnSuccessForm() in Ajax.BeginForm

Once a selection of 'Yes' or 'No' has been made on the confirmation dialog, the OnSuccessForm() function appears to not be triggered. Is there a way to pause the onBeginForm() process before moving forward? My MVC3 Form @using (Ajax.B ...

Creating a Javascript function to perform a specific action based on the selected

I am facing some challenges with my webshop project and I would appreciate any help. The main issue I'm dealing with is setting up a select box that allows customers to choose different colors of a product. When a color is selected from the dropdown, ...

The Bootstrap modal is not properly clearing all attribute properties when it is hidden

Alrighty, so I've got this modal set up: <div id="modal-container" class="modal fade" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content center-block" style="displ ...

Achieving the extraction of a particular string from an HTML element using JavaScript

<input id="WD01B3" ct="CB" lsdata="{2:'WD01B4',4:'Any',20:'\x7b\x22WDA_TYPE\x22\x3a\x22DROPDOWN_BY_KEY\x22,\x22WDA_ID\x22\x3a\x22ABCA950297D2C0C432BAB9BB ...

reverting the effects of a javascript animation

I am expanding the size of a carousel on a specific pane by adjusting its height and position. Here is how I achieve this: if(currentPane==2) { $("#carousel").animate({height:320},1000); $("#carousel").animate({top:411},1000); $("#dropShadow") ...

Can you show me how to line up a series of table rows and use jQuery to slide them up or down?

Imagine a scenario where there is a table with 50 rows, each containing parent/child relationships. The goal is to click on a parent row and expand/collapse all its children rows simultaneously by sliding them up together. How can this be achieved? Consid ...

Checking the correctness of a username through the use of AJAX, JSON, and ASP

Recently, I have started learning about json, ajax, jquery, and web services. I am currently working on a registration page where users can check the availability of a username instantly by typing in a few letters. My setup includes an HTML file for the ...

How can you implement the jquery toggle function to efficiently show or hide post replies?

I am looking to hide post replies when the page loads and then show them when a link is clicked. Here's how: HTML: <!-- The post div --> <div class="post"> <!-- The post content --> <div class="clicker">Click me!</div> ...

Modify the font style of numbers based on the keyboard language selected by the user

Is it possible to change the font family of numbers in input fields based on the user's keyboard language? For example, if the user is typing in Persian, the numbers should be displayed in a Persian font, and when they switch to an English keyboard, t ...

retrieving the webpage's HTML content from the specified URL using AngularJS

Utilizing the $http.get('url') method to fetch the content located at the specified 'url'. Below is the HTML code present in the 'url': <html> <head></head> <body> <pre style = "word-wrap: break ...

"Looking for a solution to the ESLint error related to 'no-unused-var' and Google Maps integration. How can I effectively resolve

I'm struggling with what seems to be a simple problem I tried adding /* export myMap */ or /* global myMap */ at the beginning of the script but I keep getting errors Code HTML <h1>My First Google Map</h1> <div id="googleMap" ...