How can I make a div using jQuery fade in when an input field is in

I want to create a tooltip that changes its innerHTML and fades in when an input on my form is focused. Additionally, I need to check if the tooltip has been faded in as it initially loads faded out.

In essence, I am looking to switch the tooltip content with fading effects as different inputs are focused on my form.

This is what I currently have:

<script type="text/javascript">
var visible = 0;
var nameText = "<p>May I get your<br/>full name?</p>";
var emailText = "<p>Could I please<br/>get your email<br/>address?</p>";
var messageText = "<p>What is the<br/>message you<br/>would like to send?</p>";

$('.inp').on("focus", function(event){
    if (visible == 0)
    {
        var fadeTime = 1;
        visible = 1;
    }
    else
        fadeTime = 500;

    $('bubble').fadeOut(fadeTime).html(function() {
        if ($this.attr('id') == "name")
            return nameText;
        else if ($this.attr('id') == "email")
            return emailText;
        else if ($this.attr('id') == "message")
            return messageText;
    }).fadeIn(500);

});
</script>

No JavaScript errors are being thrown. The HTML for my bubble and inputs is structured as follows:

<footer class="container">
<div class="row reveal">
    <div class="mailman fourcol">
        <img src="images/mailman.png" alt="The Mail Man" />
        <div id="bubble">
        </div>
    </div>
    <div class="contact eightcol last">

        <h1>CONTACT US FOR</h1> <h1>MORE INFORMATION</h1>
        <div class="clear"></div>
        <form name="contact-form">

            <div class="form-left">

                <div class="contact-name inp">
                    <input type="text" name="name" id="name" value="Name" />
                </div>

                <div class="contact-email inp">
                    <input type="text" name="email" id="email" value="Email" />
                </div>

                <div class="clear"></div>

                <div class="contact-message inp">
                    <input type="text" name="message" id="message" value="Message" />
                </div>

            </div>

            <div class="contact-submit">
                <a href=""></a>
            </div>

        </form>

    </div>
</div>
</footer>

Answer №1

I made some corrections for you. View the demonstration.

Here are the main issues that needed to be resolved:

  1. The focus event should be triggered on the input element. Instead of

    $('.inp').on("focus", function(event){
    , it should be
    $('.inp').on("focus", 'input', function(event){

  2. You forgot to declare var $this = $(this); before using $this.attr(id), or simply use var id = this.id; instead.

  3. $('bubble') should be corrected to $('#bubble')

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

Setting the margin to 0 auto to create a CSS Grid layout featuring an Image taking up the entire right side

I’ve been grappling with finding a consistent method to ensure uniform white space for all containers like this: https://i.stack.imgur.com/wtk0G.png Important: The grey rectangle signifies an image. My goal is for it to extend to the viewport's en ...

The Angular JavaScript code displays the message variable in the web browser

When using Angular JavaScript, the browser displays {{message}}. I have tried multiple approaches but it still doesn't work as expected. var myApp=angular.module("mod",[]); myApp.controller("con", function($scope){ $scope.message = "hhhhhhhh"; }); ...

Is it possible to load multiple angular applications within a master angular shell application?

I am searching for a method to integrate multiple Angular applications into a single shell Angular application. The concept involves having different teams develop separate Angular apps that can be loaded within a main shell app visible to end users. Thi ...

Can someone please guide me on how to transfer information from a material ui datagrid Row to a form input?

I need assistance. I have a table that holds user data and I want to create a functionality where clicking on the edit button opens a dialogue box with a form pre-filled with the user's initial data. However, I'm currently only able to pass the u ...

What is the best way to place my website's logo in the top-left corner using html and css?

Hello everyone. I am currently working on customizing a website using an HTML and CSS template for a project, but I need to make some modifications. My main goal is to add the company logo to the header, specifically in the top-left corner, however, I&apo ...

Utilizing information shared between parent and child classes to plot points on a globe

Working on an exciting project to enhance my ReactJS and ThreeJS knowledge, I encountered an issue with passing data from the parent class to functions in the child class. Here's a glimpse of my project: App.js import React, {Component} from 'rea ...

The findById function is encountering errors only on invalid id inputs: Unable to cast value to ObjectId

I have implemented a basic GET method to retrieve a 'genre' from a MongoDB database based on an id, using Mongoose. It functions correctly when a valid id is provided, but crashes the application when an invalid id is used. Here is the code snip ...

Ways to expand the dimensions of a Popup while including text

I am using a bootstrap modal popup that contains a Treeview control in the body. The issue arises when the node's text width exceeds the popup's width, causing the text to overflow outside of the popup. Is there a way to dynamically adjust the ...

Make sure to wait for the scrollTo function to finish before executing any other commands

I have a sleek scrolling directive in my AngularJS app that automatically scrolls to the bottom of the page. I want this command to execute only after the scrolling has completed. Currently, I trigger the scroll function and then use $('#comment-input ...

Updating the background-image using Jquery when a button is clicked

Is there a way to change the background image of a division using a button click without the need for a function? <input onclick="jQuery('#Div1').hide(); jQuery('#Div2').show(); jQuery('#main').css("background-image", "url ...

angularjs instructions for modifying each text value individually

How can I modify the label value for each appended field when clicking on the field? Visit this link to view the code in a plunkr. var app = angular.module('myapp', ['ngSanitize']); app.controller('AddCtrl', function ($sco ...

The function call with Ajax failed due to an error: TypeError - this.X is not a function

I am encountering an issue when trying to invoke the successLogin() function from within an Ajax code block using Typescript in an Ionic v3 project. The error message "this.successLogin() is not a function" keeps popping up. Can anyone provide guidance on ...

Using fingerprint authentication within an Android application to secure access to a web page

I'm exploring the idea of creating a wrapper application to automatically login to a webpage that requires authentication, as entering credentials on mobile can be tedious. I am the only user and I want to utilize saved credentials and the fingerprint ...

The module located at "c:/Users//Desktop/iooioi/src/main/webapp/node_modules/rxjs/Rx" does not have a default export available

I am currently delving into the realm of RxJs. Even after installing rxjs in package.json, why am I still encountering an error that says [ts] Module '"c:/Users//Desktop/iooioi/src/main/webapp/node_modules/rxjs/Rx"' has no default export ...

The image slider is blocking the dropdown functionality of the navbar on mobile devices

My code is experiencing a conflict of events. I have created a menu bar using nav bar, as well as an image slider called the caroussel. The issue arises when the window is minimized - the menu bar fails to drop down properly with the presence of the caro ...

Trick for hiding CSS elements when input field is vacant

Is there a way to hide the search result when the input is empty? While everything is functioning correctly, I would like to prevent the search result from remaining visible after clearing the input field. For example, if you type 'A' and then d ...

JavaScript threw an error with message: 'Unexpected identifier' that was not caught

Upon launching Web Developer in Firefox: SyntaxError: missing } after property list note: { was opened at line 7, column 7 ...

With Firefox, when submitting a form only the final repeated region value is $_POSTed and not all of them

There seems to be an issue with the code below when running it in Firefox. The problem occurs when passing the URL variable UserName. In Firefox, only the last record is being used from the record-set, regardless of which button is clicked. This page utili ...

Encountering an issue when adding SASS Compass Breakpoint to my development project

I'm having trouble integrating breakpoints into my web project. I have SASS, Compass, and breakpoint all installed and I've followed the installation instructions, but it seems to be causing errors. Can someone help me troubleshoot the issue? He ...

Combining httpProvider responseInterceptor with $http error callback does not function properly

In my application, I implemented a "loading screen" feature inspired by this post: 'Click' However, I am facing an issue where all $http requests are triggering the "success" callback, even for URLs that do not exist. $http.post("this doesnt ev ...