Switching the submit button to an input field with a click event

The form on my website has the following structure:

<form role="search" method="get" id="searchform" action="">
    <input type="text" name="s" id="s" placeholder="Search..." />
    <input type="submit" id="searchsubmit" />
</form>

I want the background of the submit button to change when the input field is clicked. How can I achieve this?

Answer №1

An elegant solution using only CSS:

input[type="email"]:focus + button[type="submit"]{
    border-color: blue;
}

See it in action here: http://jsfiddle.net/af23lh49/

The key to this solution is the + selector, which targets a submit button that comes immediately after a focused email input field.

Answer №2

Here's an example:

$('#input').on('click',function(){
  $('#submitBtn').css('background-color','#00f');
}).on('mouseleave',function(){
  $('#submitBtn').css('background-color','');
});

Answer №3

Witness a practical illustration that is simple and easily understood

Code Example

<section role="search" method="get" id="searchform" action="">
    <input type="text" name="s" id="s" placeholder="Search..." />
    <input type="submit" id="searchsubmit" />
</section>

Jquery Implementation

$(document).ready(function() {
  $('#s').on('focus',function(){
     $('#searchsubmit').css('background-color','red');
  });
});

Run the Code Here

Answer №4

Here's an example using the focus and blur functions together:

$('#input').focus(function(){
    $('#button').css('background-color','#f00');
}).blur(function() {
     //return to previous state
    $('#button').css('background-color','#ff0');
})

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 animation feature of the CountTo Module fails to function properly once the currency pipe is included

My end goal is to achieve a final view that looks like this: https://i.sstatic.net/VUGdb.png The values are supposed to have a counter animation from 0 to the specified final value. I have also implemented a currency pipeline in the value section for sep ...

The vertical baseline alignment is not functioning as expected

In my setup, I have a straightforward configuration: <button> Lorem </button> <input type="text" value="Ipsum"> both positioned side by side with the help of display: inline-block: button, input{ height: 34px; line-height: ...

Choosing an input option after the second ajax call is finished with JQuery

I'm fetching data from MySQL to PHP using AJAX. I am trying to implement an edit feature on my website within a modal window. The select input with options (subcategories) is loaded via AJAX after the radio input categories are loaded by a previous AJ ...

Exploring data in C# using HtmlAgilityPack to extract information from a table

I've been working on a project for some time now, and here's the situation: A friend of mine has a web application that generates data for charts using HTML. I need to extract specific values from a table on his website so that we can store this ...

Ways for enabling the user to choose the layout option

I am looking to develop a customized reporting system where users can select the specific fields they want to include in the report as well as arrange the layout of these fields. The data for the reports is sourced from a CSV file with numerous columns. Us ...

Can a single global variable be shared between a JavaScript file and a TypeScript file within an Angular project?

In my Angular 5 project, I am implementing a javascript library. If I create a global variable in my .js file, how can I access that variable from my .ts file? ...

Retrieve the data stored within an [Object Promise] for further use

Having trouble utilizing a value retrieved from an API call. Below is my TypeScript code: private async fetchPersonName() { let fullName = await Api.getName('Data($select=FullName)', `personId eq ${currentPersonId}`); ...

Ways to access the HTML content of a component in vue.js

I have a unique html template displayed below CustomTemplate.vue <template> <div class="content custom-page"> <md-card class="page-display"> <md-card-header class="page-header"> <md-card- ...

The jQuery ajax function is malfunctioning when set to synchronous mode

Can you help me with this code? I am trying to make a successful $.ajax call and return its response as the result of function a(). However, before the response from the ajax call is ready, it is returning an empty result. Any assistance would be appreci ...

The content following customized checkboxes does not align with the baseline

Here is the code snippet: <p><input type="checkbox" /> Pavement <input type="checkbox" /> Dirt or rocky trails <input type="checkbox"/> Gravel roads</p> As well as the accompanying CSS: h1, input, button, textarea, progress ...

Using Grep, transform an existing JSON object into a new JSON object by applying a grep or delete operation

Consider the JSON object below: var data = { "ok" : true, "messages" : [ { "type" : "message", "subtype" : "bot_remove" }, { "type":"message", "subtype":"file_share", ...

invoking a JavaScript function from an HTML file

I want to create a JavaScript server on Raspbian that not only serves .html content to the browser but also allows for user input through events like button clicks. Both my .html and .js files are located in the same directory, and I have used absolute pat ...

Instructions for Connecting a Bootstrap Resume to a Button

I have created a bootstrap resume that I want to integrate with a button on my portfolio website. As someone who is still learning and fairly new to this, please bear with me if this question seems odd. When I try to link it using an href tag, the button ...

Issues with Owl Carousel Slider not functioning smoothly

I made a Owl Carousel Slider with an autoplay feature, but I'm having trouble getting it to slide smoothly. Does anyone have any tips on how to make the following code run more smoothly? Javascript Code var owl = $('.owl-carousel'); owl.ow ...

Combining Versioning with BundleConfig.cs: A Comprehensive Guide

Encountering a recurring issue where changes made to CSS or Javascript files do not reflect in client browsers unless they manually refresh the page with ctrl+F5. This poses a challenge, especially in a school system with numerous users who may not be awar ...

What could be causing my text to not adapt to a mobile device screen?

Using my MacBook with a display size of 15.4-inch (2880 x 1800), I have captured screenshots of different sections of my homepage to show how they appear. #app (section1) https://i.sstatic.net/QjrXe.png #section2 (section2) https://i.sstatic.net/5HWp0. ...

What could be causing the mat-radio button in one card to toggle all other cards as well?

Within my home.component.html file, you will find the following code: <div class="grid grid-cols-5 gap-4 pt-10"> <div *ngFor="let card of cards" class=""> <div *ngIf="card==null;then nil else notnil&q ...

jQuery code cannot be executed on elements within a file loaded through an ajax call in JavaScript

Seeking guidance on resolving a persistent issue. To provide a brief overview before sharing the code: I have two files - an HTML file named "bookings.php" containing JavaScript, and a PHP file named "getBookings.php" with PHP code. The bookings.php file ...

What is the best way to incorporate the .top offset into a div's height calculation?

Looking to enhance the aesthetic of this blog by adjusting the height of the #content div to match that of the last article. This will allow the background image to repeat seamlessly along the vertical axis. I attempted the following code: $(document).re ...

Designing a div with a proportional size amidst elements of fixed dimensions

I need help figuring out how to create a dynamically sized div based on the browser's height. I'm not sure where to start or how to approach this problem. The layout I have in mind includes a 50px high header, followed by the dynamic div and the ...