How come the link isn't showing up when toggled?

After struggling with the code below, I still can't get it to work. The goal is to display a link once a file is selected so that the file can be removed (or deselected). I'm trying to figure out why the link isn't showing up as expected. Any help would be greatly appreciated!

Just for reference, I also attempted using "css", "show", and "hide". None of them were successful.

<html>

<script>
function removeFile()
{
  var input = document.getElementById("fileToUpload");
  input.value = input.defaultValue;
  refreshForm();
  return false;
}

function refreshForm() 
{
  var input = document.getElementById("fileToUpload");

  if (input.files.length > 0)
  {
    $("#upack-form > #remove-lnk").attr("visibility", "visible");
  }
  else
  {
    $("#upack-form > #remove-lnk").attr("visibility", "hidden");
  }
}
</script>

<div class="package">
  <form id="upack-form" action="/update_package" enctype="multipart/form-data" method="post">
    <label>
      <input id="fileToUpload" type="file" name="file" style="width: 280px; overflow: hidden;" single onChange="refreshForm();" />
      &nbsp;&nbsp;&nbsp;&nbsp;
      <a id="remove-lnk" style="visibility: hidden;" onclick="return removeFile();">remove</a>
    </label>
  </form>
</div>

</html>

UPDATE:

I discovered that when I moved the hyperlink inside the "label," it stopped working properly.

Answer №1

Visibility is not just a characteristic, it's actually a style property

if (input.files.length > 0)
  {
    $("#upack-form > #remove-lnk").css("visibility", "visible");
  }
  else
  {
    $("#upack-form > #remove-lnk").css("visibility", "hidden");
  }

Note: I mentioned in my previous comment that jQuery was missing, so I have included it now. You can see the working demo here

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

Angular.js fails to load successfully every other time

My angular application is running into some issues with bower. At times, when I start up the server, I encounter the following error: Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:modulerr] Failed to in ...

The design of the website is all over the place

I am encountering challenges in creating distinct containers for the header, body, and other sections of my website. The layout is not turning out as planned, and I can't pinpoint where my code is going wrong. Any advice or suggestions on how to resol ...

eliminate item from list upon user clicking the button

Does anyone have any tips on how to effectively store and remove user-selected items from an object in JavaScript? I'm encountering an issue where only the first object in the array is being removed, and not the others. Any help would be appreciated! ...

Achieve full width with flexbox column wrap while minimizing the height

How can I arrange elements in columns within a container with a fixed width and variable height, when the number of elements is unknown? My challenge is that I do not know the maximum width of the child elements, preventing me from setting specific column ...

What is the best way for a parent process to interrupt a child_process using a command?

I'm currently in the process of working on a project that involves having the user click on an 'execute' button to trigger a child_process running in the backend to handle a time-consuming task. The code snippet for this operation is shown b ...

When trying to log the parsed JSON, it unexpectedly returns undefined even though it appears to be in good

Everything was working fine until a couple of days ago. Let's consider this as my PHP script: echo json_encode(array('stat'=>'ok')); I am sending an AJAX request to this script: $.post(base_url+'line/finalize/', {t ...

Flask Modal fails to function properly in the absence of table data

I'm currently troubleshooting an issue with my modal on a Flask web app that utilizes a SQLite database. When trying to add new rows to the table using some JavaScript code, everything works perfectly unless the table is empty. In that case, I am unab ...

Typescript selection missing in Intellij IDEA when creating new Step definition File for Protractor and Cucumber

Currently working on: Protractor Cucumber and Typescript project Preferred IDE: Intellij IDEA Ultimate edition Any suggestions on what I might be overlooking? List of added plugins: 1. Javascript Support 2. Node.js ...

Tips for displaying a loading indicator above a form

I've been struggling to figure out how to display a loading indicator on top of my form without messing up the styling... https://i.sstatic.net/FFCRW.png My goal is to show the loading indicator when any action, like deleting an item or changing qua ...

The button should only be visible when the input is selected, but it should vanish when a different button within the form is

Having an issue with displaying a button when an input field is in focus. The "Cancel" button should appear when a user interacts with a search bar. I initially used addEventListener for input click/focus to achieve this, but ran into a problem: on mobile ...

How to sort by a nested field in Mongoose

My task involves sorting data using a nested field named orderIndex. router.get("/", (req, res) => { Book.find({ _id: req.params.id }) .sort({ 'Book.chapters.orderIndex': "asc" }) // This part is not functioning as expected .then ...

What is the best way to use JavaScript to fill in missing images with alternative HTML content?

As a provider of a service that helps users find the location of pictures, I face a challenge due to the fact that the pictures are stored on another internal site. Some users may not have access to this site as my service offers additional information. Th ...

What is the best way to implement an object literal method for making an HTTP GET request to retrieve JSON data?

I'm facing an issue with the HTTP GET method when trying to retrieve JSON data. Although the HTTP GET method is successfully fetching the JSON data, I'm struggling to connect it with the object literal. For better clarity, here's the specif ...

Testing a cucumber scenario by comparing the redirect URL with a different URL

Currently, I am working on writing Cucumber tests for my project. One issue I have encountered is that when clicking a certain button in my code, it redirects to another page with a fixed URL. How can I retrieve the current URL within the Cucumber test? I ...

The database threw an error: "Duplicate key found in collection causing MongoError: E11000"

I'm currently in the process of updating an object within an array found in a document "state": [], "users": [{ "ready": false, "_id": { "$oid": "5fb810c63af8b3 ...

Modifying properties within child components

Within my parent Vue Page, I have inserted a FormInput component inside my form. new.vue <b-form @submit.prevent="submit"> <FormInput :name="name"/> <b-button @click="submit">Save</b-button> <b-form> <script> i ...

Managing information from forms that are generated dynamically

My idea involves creating multiple forms dynamically and using jQuery to handle their submission. To generate the forms, I would use PHP code similar to the following: while(some condition) { echo "<form method=post action=specific_url.php name=so ...

Why must I handle Access-Control-Allow-Origin issues with XMLHttpRequest() but not with forms?

When attempting to make a request using XMLHttpRequest(), I encounter the Access-Control-Allow-Origin error. However, when I use a form like the one below, no issues arise. Is there something about forms that allows access? What could be the reason for t ...

Drop-down Sidebar Menu in Bootstrap 4

Could someone provide guidance on creating a Bootstrap 4 Sidebar Menu Dropdown similar to the one shown in the image below? https://i.sstatic.net/NZ8B8.jpg I am considering using Dropright buttons but have been unsuccessful in finding good examples of co ...

Set up a WhatsApp web bot on the Heroku platform

I am currently in the process of developing a WhatsApp bot using the node library whatsapp-web.js. Once I finish writing the script, it appears something like this (I have provided an overview of the original script) - index.js const {Client, LocalAuth, M ...