Tips for dynamically hiding/showing a button depending on the focus of two different HTML inputs

I'm working on a login section and looking to add a unique feature:

When a user selects the email or password input field, I want the button text to change to 'LOGIN'. If neither input field is selected, the text should display 'NOT A USER YET?'

The issue arises when the email input loses focus and the password input gains focus immediately after.

https://i.sstatic.net/0IcKL.png
(source: firefoxusercontent.com)

https://i.sstatic.net/0j7KE.png
(source: firefoxusercontent.com)

This is what I have attempted so far:

The appearance is not essential, only the JavaScript functionality matters

HTML:

<form action="" method="post">
    <input type="text" name="email" placeholder="Email" style="width: 120px;height:25px;line-height: 1.1;position: absolute;left: 0;top: 0;border-radius: 3px 0 0 0" class="input-xs form-control lg_email no-shadow-on-focus">

    <input type="password" name="password" placeholder="Password" style="width: 120px;height:25px;line-height: 1.1;position: absolute;left: 0;top: 25px;border-radius: 0 0 0 3px" class="input-xs form-control lg_psw no-shadow-on-focus">

    <button type="submit" style="width:80px;text-align: center;font-size: 15px;height:50px;line-height: 25px;border-left: none;background-color: blue;display: none;position: absolute;right: 0;top:0;border-radius: 0" class="btn btn-lg btn-primary bgLogin">LOGIN</button>

    <button class="f-11 btn bgUser btn-primary" style="width:80px;text-decoration: none;height: 50px;position: absolute;right: 0;top:0;border-radius: 0 3px 3px 0"><span style="letter-spacing: 2px" class="f-13">NOT A</span><br> user yet ? </button> 

</form>

JS

setInterval(function(){
    if($(".lg_email").is(":focus") || $(".lg_psw").is(":focus")){
        $(".bgLogin").show();
        $(".bgUser").hide();
    }else{
        $(".bgLogin").hide();
        $(".bgUser").show();
    }
},500);

In this way, there is a slight delay before the action triggers.

Answer №1

How about trying something like this:

$(function()
{

    $(".lg_email, .lg_psw").on('focus',function()
    {
       $(".bgLogin").show();
       $(".bgUser").hide();
    });


    $(".lg_email, .lg_psw").on('blur',function()
    {
         $(".bgLogin").hide();
         $(".bgUser").show();
    });
});

This method utilizes direct events instead of relying on intervals.

Answer №2

Check out this helpful solution: https://jsfiddle.net/mzvarik/f6g8bau5/

Instead of using <form action="">, simply use <form>. It used to have issues in the past, but now it's unnecessary if left empty.

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

What is the process for extracting dates in JavaScript?

I need help extracting the proper date value from a long date string. Here is the initial date: Sun Aug 30 2020 00:00:00 GMT+0200 (Central European Summer Time) How can I parse this date to: 2020-08-30? Additionally, I have another scenario: Tue Aug 25 ...

Tips for invoking a custom function in jQuery with a delay when hovering

I've been struggling with my syntax, trying to achieve something simple. I have a function called 'displayUserinfo' that pops up a box with user information. It is triggered when the user clicks on an element with the class 'avatar&apo ...

Utilizing AngularJS to show content based on regular expressions using ng-show

With two images available, I need to display one image at a time based on an input regex pattern. Here is the code snippet: <input type="password" ng-model="password" placeholder="Enter Password"/> <img src="../close.png" ng-show="password != [ ...

When incorporating MDX and rehype-highlight on a next.js site to display MD with code snippets, a crash occurs due to Object.hasOwn

I'm encountering an issue with my setup that is based on examples from next.js and next-mdx-remote. Everything was working fine until I added rehypeHighlight to the rehypePlugins array, which resulted in this error. Any thoughts on why this could be h ...

Blur effect on backdrop-filter causing shadowy inset borders (specific to Chrome and Windows)

Note: The issue mentioned below has been resolved in the latest version of Chrome (119.0.6045.200). While transitioning on backdrop-filter, I have observed a dark inset shadow that is only visible during the transition. This behavior seems to occur only ...

Issue with iOS 10: Changes to class not reflected in CSS/styles

Currently, I am utilizing Ionic with Angular to develop an application for Android and iOS. Surprisingly, everything functions smoothly on Android, but there seems to be an issue with iOS. I am employing a class change on an element using ng-class. I can ...

After the execution of the script by V8, which phase will be initiated first?

Scenario // test.js setTimeout(() => console.log('hello'), 0) setImmediate(() => console.log('world')) Simply execute node test.js using node v12.12.12 on an Intel MacBook Pro. The output may vary: hello world Sometimes it is: ...

Why is JavaScript globally modifying the JSON object?

I have a few functions here that utilize the official jQuery Template plugin to insert some JSON data given by our backend developers into the variables topPages and latestPages. However, when I use the insertOrHideList() function followed by the renderLis ...

implement an angular directive to apply a CSS element

I am utilizing AngularJS and ng-repeat to populate a dynamic list of studies. This list has the capability to toggle into child elements of each item, creating an accordion-style toggle list that can go up to three levels deep for each list item. I am curr ...

Typescript: Verifying the type of an interface

In my code, I have a function called getUniqueId that can handle two different types of interfaces: ReadOnlyInfo and EditInfo. Depending on the type passed to this function, it will return a uniqueId from either interface: interface ReadOnlyInfo { item ...

What is the process for building an interactive quiz using JavaScript?

In the process of creating a quiz, I envision a user interface that presents questions in a "card" format. These questions will include simple yes/no inquiries and some multiple-choice options. As the user progresses through the quiz, their answers will de ...

Using one payload.js file for all Nuxt static generated routes with identical data

I am facing a challenge with my NuxtJS site where I have only one page /pages/matrix/index.vue but numerous dynamic routes pointing to this page, all utilizing the same data set. During static build generation for deployment on Netlify, the size of the dis ...

Having trouble with jQuery events not triggering properly after dynamically inserting elements using an ajax request?

It's strange that all my jQuery events become unresponsive after an AJAX call. When I use a load function, once the JSP reloads, none of the events seem to work properly. Any suggestions? Below is the code that triggers the function call: $('#p ...

How to invoke a function from a different ng-app in AngularJS

I have 2 ng-app block on the same page. One is for listing items and the other one is for inserting them. I am trying to call the listing function after I finish inserting, but so far I haven't been successful in doing so. I have researched how to cal ...

What is the best way to customize the color of the border and icon in an outlined MaterialUI input field

I'm a newcomer to materialUI and I have a question regarding customizing an outlined input field with an icon. Due to my dark background, I need the icon, border, and text color to be lighter, such as grey. Can someone please advise on the materialUI ...

Discover the Magic of Angular 8 and Bootstrap 4 Tooltips

I'm trying to implement tooltips from Bootstrap 4 into my web application. According to Bootstrap documentation, I should initialize the tooltips with the following code: $(function () { $('[data-toggle="tooltip"]').tooltip() }) (Implement ...

Generate a table framework by dynamically adjusting the number of rows and columns

I'm running into an issue with my implementation of nested for-loops to dynamically generate a table using JavaScript. For this particular scenario, let's assume numRows = 2 and numCols = 6. This is the code snippet in question: let table = $( ...

What is the method for executing a server-side script without it being transmitted to the browser in any way?

Although the title may be misleading, my goal is to have my website execute a php file on the server side using arguments extracted from the html code. For example: document.getElementById("example").value; I want to run this operation on the se ...

Methods to Maintain Consistent HTML Table Dimensions utilizing DOM

I am facing an issue with shuffling a table that contains images. The table has 4 columns and 2 rows. To shuffle the table, I use the following code: function sortTable() { // Conveniently getting the parent table let table = document.getElementById("i ...

What are the steps to transforming a regular expression into a dynamic format?

I've developed a powerful regex that locates specific text within a lengthy string without spaces. Now I'm attempting to utilize it dynamically to search for various words, but I can't seem to make it function as intended. Check out my rege ...