Conceal an element using JQuery

^ no, it does not answer the question at all

I have a total of 4 input fields and 4 spans. Each span displays the value from the corresponding input field.

My goal is to hide empty spans. Simply removing the string from the input box leaves the span blank, but I want it to be completely hidden. Currently, when you leave Field 3 empty, it creates an empty space between Field 2 and Field 4. I would like Field 4 to move up into the place of Field 3 instead.

Is there any way to achieve this?

<tr>
 <td><h2>Field1</h2>
<input type="text" value="0" class="field1" type="number" min="0" max="20" maxlength="2" size="5"></td>
<td><h2>Field 2</h2>
<input type="text" value="101" class="field2" maxlength="4" size="5"></td>
    <td><h2>Field3 - hide span when im empty</h2>
<input type="text" value="0" class="field3" type="number" min="0" max="20" maxlength="2" size="5"></td>
<td><h2>Field 4</h2>
<input type="text" value="101" class="field4" maxlength="4" size="5"></td>
</td>
  </tr>
    <br><br>

    Field1: <span class="genaug"><span class="field1"></span>%</span><br />
        Field2: <span class="genpd"><span class="field2">0</span></span><br />
        <b><span class="genaug"><span class="field3"></span></span><br /></b>
Field4: <span class="genpd"><span class="field4">0</span></span>

$("input.field1").on("keyup",function () {
    $("span.field1").html($(this).val());
});
$("input.field2").on("keyup",function () {
    $("span.field2").html($(this).val());
});
$("input.field3").on("keyup",function () {
    $("span.field3").html($(this).val());
});
$("input.field4").on("keyup",function () {
    $("span.field4").html($(this).val());
});

if ("input.Field3".length) span.style.display = "none";

JSFiddle updated with craig1231 solution http://jsfiddle.net/jRwDg/7/

Answer №1

$("input.field1").on("keyup",function () {
    var value = $(this).val();
    if (value == "") {
        $("span.field1").hide();
    }
    else {
        $("span.field1").show().html(value);
    }
});

Answer №2

Here's an example for you to try:

if($("input.field3").val()=="") $("span.field3").css("display", "none");

If spaces are not important, you can use the following code snippet:

$.trim($("input.field3").val())

Answer №3

One way to validate the field length is by using the key up event.

$("input.field3").on("keyup",function () {
    if ($(this).val().length == 0)
        $("span.field3").hide();
    else
        $("span.field3").show().html($(this).val());
});

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

In Android Kitkat 4.4.4, the Ionic navbar displays icons vertically when using the <ion-buttons end> feature

When viewing in the browser with ionic serve, everything looks fine. However, on an Android Kitkat device running version 4.4.4, the buttons on the right side of the navbar are displaying vertically instead of horizontally. <ion-navbar> <ion-ti ...

Utilizing AJAX and javascript for extracting information from websites through screen scraping

Is it possible to scrape a screen using AJAX and JavaScript? I'm interested in scraping the following link: I tried the technique mentioned on w3schools.com, but encountered an "access denied" message. Can anyone help me understand why this error is ...

Enhance the interoperability of Babel with Express.js by steering clear of relative

My current approach to imports is as follows: import router from '../../app/routes' Is there a way to avoid using ../../, for example: import router from 'app/routes'? In typescript, I can achieve this with the following configuratio ...

The JSONP request connected to the user input field and button will only trigger a single time

Hello all, I'm new to this and have been searching the internet high and low to see if anyone has encountered a similar issue before, but haven't had any luck. I've been attempting to set up a JSONP request to Wikipedia that is connected to ...

Redirect to a randomly selected website from an array of links after a certain period of time

Here is some code snippet that I'm working with: jQuery(document).ready(function() { var textArray = [ 'www.yahoo.com', 'www.wikipedia.org' ]; var randomNumber = Math.floor(Math.random()*textArray.length); link.se ...

How can I make a Vue component close when clicking outside of it?

I am searching for a solution to automatically close a component when there is a click outside of the element. I attempted to use an addEventListener for this purpose. Although it successfully closes the component, it encounters an issue where it fails to ...

Using node.js to populate embedded document in Mongoose database model

Scenario: const productSchema = new Schema({ name: String, details : [{ name: String, description: String }] }) const listSchema = new Schema({ name: String, products: [{ product_id: { type: Schema.Types.ObjectId, ref: 'Product' } ...

Safari has no issues running Javascript, but other browsers are encountering failures

I am facing an issue where the code is working on Safari but failing on other browsers, and I can't figure out why. You can find the html part and the main javascript part. The main issue at hand is: When executing the function downloadurl(url, fun ...

There was a glitch encountered while constructing (Verifying type validity) with Prisma

There was an issue in the node_modules/@prisma/client/runtime/library.d.ts file on line 1161, specifically error TS1005 where a '?' was expected. 1161 | select: infer S extends object; | ^ 1162 | } & R ...

Does Transclude eliminate tr and td elements from the content?

I'm encountering an issue with my angularjs application where the tr and td tags are mysteriously disappearing. angular.module('transcludeExample', []) .directive('pane', function() { return { restrict: 'E' ...

What is causing the lack of redirection with this particular "res.redirect()" function?

How can I successfully implement a redirection in a Node.js and Express.js application? When I attempt to redirect by clicking a button, only the URL changes without any further action taking place. Using a form with a button inside, the form specifies a ...

Adapt the stylesheet for mobile devices using CSS

I am struggling with updating file paths in CSS depending on whether the end user is accessing my site from a PC or mobile device. Below is my CSS code, where I attempted to redirect users if they are on a handheld device: <link rel="stylesheet" type=" ...

What is the best approach to incorporate this D3.js HTML element into my AngularJS project for easy manipulation?

I've been experimenting with creating graphs using the D3.js graphical library for Javascript on my webpage. Currently, I can draw a gray line on an SVG using HTML and a red line using Javascript on a new SVG container created in Javascript. However, ...

The issue with the jQuery accordion effect and clearInterval causing errors

Is there a downside to using a timer in the accordion script I'm currently utilizing? It seems that even when a user navigates to another section on the webpage, the timer continues to run in the background. Additionally, if the user clicks on the fun ...

Having trouble transferring the file from HTML to PHP during the upload process

I am encountering an issue where I cannot see the file when trying to check if it has been uploaded or not. Every time I select the file, it prints out a message saying "please select the file." Can someone assist me with this problem? <html> &l ...

What is the best way to create a mirror effect on one div by clicking another div?

I have created a schedule grid and I am looking for a way to allow users to click on the UK hour and then have the corresponding U.S time highlighted. Is there a CSS solution for this? The functionality I need is to be able to select multiple hours. I have ...

Manipulating information from one format to another

I am currently tackling the task of calculating scores based on departments within groups. For simplicity, I will focus on just one group as an example. Here is the data structure that I have: const data = [{ "id": "cklt7ln1k0922o0sabjkk74m9", ...

What's the best way to ensure that the iframe resolution adjusts dynamically to perfectly fit within its container?

iframe-screenshot Displayed in the image are two iframes, but at times I may need to display three. The aim is to ensure that all iframes are responsive within their containers. Below is my existing CSS styling: iframe { width: 100%; height: 1 ...

Transferring data from PHP to an HTML function

I have attempted to retrieve the coordinates generated by the geocode and passed them to the function initialize in order to display a map with the location. Despite transferring the variables from PHP to the function initialize(), it seems that the lati ...

Dealing with illegal characters, such as the notorious £ symbol, in JSON data within a JQuery

I'm encountering an issue with a textarea and the handling of special symbols. Specifically, when I use $('#mytextarea').val() to retrieve text that contains '£', I end up seeing the black diamond with a question mark inside it. T ...