Modify the appearance of the submit button when the input field is focused

Is there a way to change the button color when an input field is in focus? I'm open to using either JavaScript or CSS for this. Thanks in advance for any assistance.

.redbutton {
  background-color:red;
  }
<form accept-charset="UTF-8" method="post" action="http://go.pardot.com/l/213532/2016-08-08/jr9" class="form" id="pardot-form">

  <p class="form-field  first_name pd-text required required-custom">
    <label class="field-label" for="213532_2892pi_213532_2892">First Name *</label>
    <input type="text" name="213532_2892pi_213532_2892" id="213532_2892pi_213532_2892" value="" class="text" size="30" maxlength="40" onchange="" />
  </p>

  <p class="form-field  last_name pd-text required required-custom">
    <label class="field-label" for="213532_2894pi_213532_2894">Last Name *</label>
    <input type="text" name="213532_2894pi_213532_2894" id="213532_2894pi_213532_2894" value="" class="text" size="30" maxlength="80" onchange="" />
  </p>

  <p class="form-field  email pd-text required required-custom">
    <label class="field-label" for="213532_2896pi_213532_2896">Email *</label>
    <input type="text" name="213532_2896pi_213532_2896" id="213532_2896pi_213532_2896" value="" class="text" size="30" maxlength="255" onchange="piAjax.auditEmailField(this, 213532, 2896, 14168);" />
  </p>


  <p class="submit">
    <input type="submit" accesskey="s" value="SUBMIT" />
  </p>


</form>

Answer №1

To determine the current state using jQuery's focus and blur callbacks, follow this script:

$("input[type=text]").on("focus blur", function() {
    if( $("input[type=text]:focus").length > 0 ) {
        $("input[type=submit]").addClass("redbutton");
    }
    else {
        $("input[type=submit]").removeClass("redbutton");
    }
});
.redbutton {
  background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form accept-charset="UTF-8" method="post" action="http://go.pardot.com/l/213532/2016-08-08/jr9" class="form" id="pardot-form">

  <p class="form-field  first_name pd-text required required-custom">
    <label class="field-label" for="213532_2892pi_213532_2892">First Name *</label>
    <input type="text" name="213532_2892pi_213532_2892" id="213532_2892pi_213532_2892" value="" class="text" size="30" maxlength="40" onchange="" />
  </p>

  <p class="form-field  last_name pd-text required required-custom">
    <label class="field-label" for="213532_2894pi_213532_2894">Last Name *</label>
    <input type="text" name="213532_2894pi_213532_2894" id="213532_2894pi_213532_2894" value="" class="text" size="30" maxlength="80" onchange="" />
  </p>

  <p class="form-field  email pd-text required required-custom">
    <label class="field-label" for="213532_2896pi_213532_2896">Email *</label>
    <input type="text" name="213532_2896pi_213532_2896" id="213532_2896pi_213532_2896" value="" class="text" size="30" maxlength="255" onchange="piAjax.auditEmailField(this, 213532, 2896, 14168);" />
  </p>

  <p class="submit">
    <input type="submit" accesskey="s" value="SUBMIT" />
  </p>
</form>

Answer №2

Give this a shot

$(document).ready(function(){
          $("input").focus(function(){
            $("input[type='submit']").addClass("changeBg");
        });
          $("input").focusout(function(){
            $("input[type='submit']").removeClass("changeBg");
        });
        });
.redbutton {
          background-color:red;
          }
        .changeBg{background:yellow;}
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
          <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <form accept-charset="UTF-8" method="post" action="http://go.pardot.com/l/213532/2016-08-08/jr9" class="form" id="pardot-form">

          <p class="form-field  first_name pd-text required required-custom">
            <label class="field-label" for="213532_2892pi_213532_2892">First Name *</label>
            <input type="text" name="213532_2892pi_213532_2892" id="213532_2892pi_213532_2892" value="" class="text" size="30" maxlength="40" onchange="" />
          </p>

          <p class="form-field  last_name pd-text required required-custom">
            <label class="field-label" for="213532_2894pi_213532_2894">Last Name *</label>
            <input type="text" name="213532_2894pi_213532_2894" id="213532_2894pi_213532_2894" value="" class="text" size="30" maxlength="80" onchange="" />
          </p>

          <p class="form-field  email pd-text required required-custom">
            <label class="field-label" for="213532_2896pi_213532_2896">Email *</label>
            <input type="text" name="213532_2896pi_213532_2896" id="213532_2896pi_213532_2896" value="" class="text" size="30" maxlength="255" onchange="piAjax.auditEmailField(this, 213532, 2896, 14168);" />
          </p>


          <p class="submit">
            <input type="submit" accesskey="s" value="SUBMIT" />
          </p>


        </form>

Answer №3

Alter the code below to modify the background-color of the submit button when any field is focused

$("input").not("input[type=submit]").focus(function(){
      $("input[type=submit]").css("background-color","green")
    })

Restore the background-color of the submit button if a field loses focus

$("input").not("input[type=submit]").blur(function(){
  $("input[type=submit]").css("background-color","")
})

See it in action on JSFiddle

Answer №4

Here's a way to achieve this:

I opted for a more detailed approach when selecting the items, but feel free to simplify if you prefer. I just wanted to provide clarity on the selection process.

Additionally, let's update the input type from text to email field by using input type="email"

$("p:not(:last-child) input").focus(function() {
     $(this).parent("p").siblings("p").find("input[type='submit']").addClass("redbutton")
     
});
$("p:not(:last-child) input").focusout(function() {
    $(this).parent("p").siblings("p").find("input[type='submit']").removeClass("redbutton")
 });
.redbutton {
  background-color:red;
  }
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form accept-charset="UTF-8" method="post" action="http://go.pardot.com/l/213532/2016-08-08/jr9" class="form" id="pardot-form">

  <p class="form-field  first_name pd-text required required-custom">
    <label class="field-label" for="213532_2892pi_213532_2892">First Name *</label>
    <input type="text" name="213532_2892pi_213532_2892" id="213532_2892pi_213532_2892" value="" class="text" size="30" maxlength="40" onchange="" />
  </p>

  <p class="form-field  last_name pd-text required required-custom">
    <label class="field-label" for="213532_2894pi_213532_2894">Last Name *</label>
    <input type="text" name="213532_2894pi_213532_2894" id="213532_2894pi_213532_2894" value="" class="text" size="30" maxlength="80" onchange="" />
  </p>

  <p class="form-field  email pd-text required required-custom">
    <label class="field-label" for="213532_2896pi_213532_2896">Email *</label>
    <input type="email" name="213532_2896pi_213532_2896" id="213532_2896pi_213532_2896" value="" class="text" size="30" maxlength="255" onchange="piAjax.auditEmailField(this, 213532, 2896, 14168);" />
  </p>


  <p class="submit">
    <input type="submit" accesskey="s" value="SUBMIT" />
  </p>


</form>

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

The Facebook API's JavaScript SDK displays the status as 'connected' even after logging out

As I navigate my AngularJS website, I am utilizing the Facebook SDK for JavaScript to facilitate registration forms. After successfully logging in and retrieving the necessary data from my first attempt, I proceeded to register and eventually logged out of ...

What is the best way to retrieve combined reducers in react-redux?

I am currently experimenting with react-redux to manage states and props in a small project. Take a look at this example code: (Index.js) import React, { Component } from 'react' import { render } from 'react-dom' import { Provider ...

Developing a side panel for navigation

My goal is to create a sidebar that shifts to the right from the left side and makes space on the page when the hamburger menu is pressed. I have made progress in achieving this, but I am encountering difficulties with the animation. const btnToggleSide ...

How can I use Angular 6 to design an interactive user interface with drag-and-drop functionality?

I am looking to make a dynamic form using Angular 7 with drag and drop functionality. The controls I need for building the form are: Check Boxes Multiple Headings Sub Headings Table + Formatted Tables + Unformulated Checkbox + text Text Fields + formatte ...

Steps for transforming a JSON into a Class to generate objects

My JSON data is displayed below: var nodeclienthash={ socket:null, init : function() { // Initializing socket.io this.socket = new io.Socket(this.config.host, {port: this.config.port, rememberTransport: false}); } }; I am currently looking t ...

Vue JS - Issue with data reactivity not being maintained

Currently, I have implemented a pagination indicator that displays the number of results on each page. For instance, page 1 shows '1-5' and page 2 shows '6-10 out of 50 results', and so on. The logic for updating the results seems to b ...

The angular2 error message indicating a property cannot be read if it is

Encountering an issue trying to utilize a method within an angular2 component. Here's the code snippet : import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { AgGridModule } from &ap ...

Error encountered with STRIPE PAYMENT GATEWAY: "Customer cus_xxxx is missing card with ID tok_visa"

// client side code const onToken = async (token) => { console.log(token); const bookingDetails = { room, userid: JSON.parse(localStorage.getItem("currentUser"))._id, fromdate, todate, totalAmount, totaldays: totalDays, token, }; try { ...

How to access the directive object within the controller in AngularJS

Imagine I have a basic directive defined like this: app.directive('someDirective', [function() { return { restrict: 'E', link: function() { }, controller: [function() { // Want to access ...

Ways to have Express display a randomly selected question from my personal JSON file

I have set up a personal server to enhance my backend coding skills. Currently, it is in its initial phase. I have developed a basic express application where I can import and display a JSON file. However, I am facing a challenge with automatically loading ...

Avoiding redundancy by establishing the loading state in a redux reducer

Let's dive into a concrete example to better illustrate my point. In the webapp I'm working on, users can apply for jobs using a job reducer that handles various actions such as creating_job, created_job, fetching_job, fetched_job, fecthing_jobs, ...

Is there a method to make this package compatible with Angular version 16?

I recently integrated the ngx-hotjar package version 11.0.0 into my Angular 10 project with success. However, when trying to use it in a new Angular 16 project, I encountered the following error during ng serve: Error: src/app/app.module.ts:275:12 - error ...

Initiating a web-based compiler directly through a browser using any method possible

Currently, I am developing a web-based educational tool that allows students to view code examples in a browser and edit them directly within the browser. I have been exploring ways to incorporate a client-side compiler so that they can run and debug the c ...

Struggling to locate the earliest date within a nested array of objects

I have an array of objects nested inside another object. My goal is to identify the minimum date from the 'FromDate' property within the 'Market' objects. What would be the most effective method to achieve this when dealing with both si ...

Searching for a way to retrieve all information based on the parent in a dynamically dependent Laravel system?

Is there a way to fetch all child data by default in a dropdown list, but still have the option to select parent data when needed? I am using AJAX and I'm new to it. Edit: I have a 'category' dropdown with dynamically loaded parent data fro ...

How can I set a specific value for a variable that is located within an array?

For instance: var x = y = z = null; var list = [x, y, z]; var number = 10; Is there a way to assign number to x using list, without replacing list[0]? ...

The term 'TextInput' is not recognized in React Native and cannot be found

I'm currently working on setting up a login screen for a social media app that I am developing. The issue I am facing is that I have a LoginScreen.js and RegisterScreen.js with forms for email and password, but when I try to render them, I encounter a ...

Navigate to a new page seamlessly without the need for refreshing the current page in Django

Is there a way to navigate to another page without refreshing after clicking on a link to a different URL? For example, if I have a list of books displayed and I click on one of the books, can it redirect me to the selected book page without reloading the ...

Is there a never-ending loop caused by a jquery/jqtouch getJSON call?

I'm currently working on my first JQTouch application. I've encountered an issue where, upon transitioning from #login to #home, a JSON ajax call is triggered successfully but it seems that the pageAnimationEnded event gets stuck in an infinite l ...

How can I ensure that my content stays fixed at the bottom of a scrolling div in Material UI React?

I have developed a React JS component using Material UI that includes a chat input feature. However, I am facing an issue where the width of the chat input is always half the screen size on desktop or mobile devices. When I try to change the width to 100%, ...