Issue with To-Do list: Can't move items to the right side for closing the list

I'm having difficulty positioning the "close" button to the right side.

Even after attempting various CSS properties like float:right, right:0, and align-content:right, I still can't seem to get it in the proper position. index.html

<head>
<style>
.close{
right:0;
}
</style>
</head>
<body>
<div> 
<input type = "text" size = "35" id="work" placeholder = "Enter your list here">
<input type="submit" onClick="appendToDoList()" value="Add to List">
</div>
<ul id="workUl">
<li>Work out</li>
</ul>
<script>
//include a close button for list 
    var getList= document.getElementsByTagName("LI");
    var i;
    for(i=0; i<getList.length; i++){
        var span = document.createElement("SPAN");
        var closebtn = document.createTextNode("\u00D7");
        span.appendChild(closebtn);
        getList[i].appendChild(span);
    }

    //add to the list   
    function appendToDoList(){
    var ListN = document.createElement("li");
    var N = document.getElementById("work").value;
    var t = document.createTextNode(N);
    ListN.appendChild(t);
    if (N === '') {
    alert("You must write something!");
    } else {
    document.getElementById("workUl").appendChild(ListN);
  }
   document.getElementById("work").value = "";

  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  var i;
  for(i=0; i<close.length;i++){

  }
  ListN.appendChild(span);
}   

The close button is supposed to be aligned to the right side. Can someone please offer some suggestions? I've also searched through similar posts in the past, but nothing seems to work for me.

Answer ā„–1

Ensure your code is well-organized and structured properly. I have made a few minor adjustments to the fiddle, including positioning the close "button" on the right side (https://jsfiddle.net/x1kf8mz6/)

This is the HTML:

<!DOCTYPE html>
<html>
<body>
  <div> 
    <input type = "text" size = "35" id="work" placeholder = "Enter your list here">
    <input type="submit" onClick="appendToDoList()" value="Add to List">
  </div>
  <ul id="workUl">
    <li>Work out</li>
  </ul>
</body>
</html>

Here is the JS part:

//function to add items to the list   
function appendToDoList(){
  var ListN = document.createElement("li");
  var N = document.getElementById("work").value;
  var t = document.createTextNode(N);
  ListN.appendChild(t);
  if (N === '') {
    alert("You must write something!");
  } else {
    document.getElementById("workUl").appendChild(ListN);
  }
  
  //create the 'close' button for each item
  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  ListN.appendChild(span);
}   

And lastly, the CSS:

.close{
  right:0;
}

Answer ā„–2

To ensure proper functionality, be sure to designate the span tag's position as "absolute" and the li tag's position as "relative". This configuration will guarantee successful operation.

Answer ā„–3

Consider implementing this CSS solution (worked for me after understanding your query)

    ul li {
  position: relative;
  padding: 5px 5px 5px 5px;
}

.close {
  position: absolute;
  right: 0; //Adjust the icon's distance (*e.g. add 5em to bring it closer*)//
  top: 0;
}

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

Is data shared globally for each HTTP/Session request?

QUERY: Is there a method to establish a variable storage specific to each session or HTTP request? The variable should be accessible globally within that session/request/connection, without the need to pass it between functions. For instance (as an examp ...

Master the art of using Insertion Sort in javascript with the help of Khan Academy

Seems like I am almost there with solving this problem, but my code isn't running as expected. Can someone offer some feedback and point out where I went wrong? var insert = function(array, rightIndex, value) { for(var j = rightIndex; j & ...

The labels on the bar graph ticks are failing to update in d3.js when the data is modified

My complete working code is below: const outerWidth = 600; const outerHeight = 300; const margin = 60; const height = outerHeight - 2*margin; const width = outerWidth - 2*margin; var data = [ { group: 'A', value: 5 }, { group: 'B', ...

Displaying object properties within another object obtained from an API request in a React component

Dealing with API data can be tricky, especially when there are nested objects involved. I've encountered an error message stating 'undefined is not an object (evaluating 'coin.description.en')', as the description property of the c ...

javascript alter css property display to either visible or hidden

I am struggling with a CSS id that hides visibility and uses display: none. The issue arises when I want the element to be visible upon clicking a button, but removing the display:none property is causing design problems due to it being an invisible elemen ...

The compatibility of jQuery is not guaranteed in a file obtained through a Java select include statement

I am encountering an issue with a simple form that includes a PHP file in a div when changed, but for some reason, jQuery does not load when placed in the included file. Can someone please help me understand why this is happening? <select name=&a ...

"What is the best way to display an error message when a submit button is clicked

Is there a way to display an error message upon clicking the submit button? I am encountering an issue where I'm seeing an error message after running the program (when I include 'required: true'). Ideally, I would like the error message to ...

Adding fields to all objects in an array within MongoDB

I'm trying to update all objects in an array by adding a new field only if it doesn't already exist. I attempted to use the updateMany method but it doesn't seem to be working. Can someone please assist me? const updateResult = await Form.up ...

The function cannot be called on a type that does not have a callable signature. The specified type, 'number | Dispatch<SetStateAction<number>>', does not have any compatible call signatures

Currently, I am working on setting up state to be passed through context in React using hooks. However, when I attempt to use the dispatched state updater function, an error is thrown: Cannot invoke an expression whose type lacks a call signature. Type &a ...

sequenced pauses within a sequence of interconnected methods in a class

scenario: There are two javascript classes stored in separate files, each utilizing a different external service and being called within an express.js router. Refer to the "problematic code" section below: route routes.post('/aws', upload.sing ...

I'm having trouble getting a letter grade from my JavaScript code

I created a JavaScript function that should display a letter grade once the average of 5 subjects is calculated, but for some reason, it's not working as expected. I'm feeling quite lost at the moment. The fourth function I implemented isn' ...

Implement a feature in Vue.js where clicking a button changes the background color

I'm working on an app using VUE JS and incorporating various components. Within one of the components, I have a button that I want to change color when clicked. This button is responsible for selecting different items from a table, so I'd like i ...

The error message that popped up on my console read: "Uncaught (in promise) DOMException: Failed to load due to the absence of a compatible source."

Whenever I attempt to create a JavaScript function that allows users to preview their files before submission, I encounter this error: Uncaught (in promise) DOMException: Failed to load because no supported source was found. My goal is to develop a JavaScr ...

Locating the index of the initial duplicate in an array using Javascript

const numbers = [2, 4, 5, 2, 3, 5, 1, 2, 4]; Iā€™m looking to write a function called indexOfRepeatedValue (array) that utilizes the numbers stored in the provided variable. The function should create a variable called firstIndex. Within a for loop, deter ...

When running `grunt serve: dist`, an error is thrown stating: "Unknown provider: utilProvider <- util <- NavbarController"

I am facing a problem with my angularJS website that is built using the yeoman angular-fullstack generator. When I run grunt serve, everything works perfectly fine. However, when I try to run grunt serve:dist, I encounter this error: grunt serve: dist -&g ...

Add or delete elements in a hyperlink

Currently, I'm utilizing to facilitate the filtering of various items on a website. The menu needs to function as a "build up" system where selecting one category filters to that specific category, clicking on another category adds it to the existing ...

Executing a SQL query using a JavaScript variable as a parameter

I am currently working on a website form that includes a select menu populated with data from an SQL table using a loop. The form is being displayed using JavaScript scripts, which are functioning perfectly. However, I am facing an issue in the final step ...

Tips for managing the onloadedmetadata event

If a video file cannot be played by the browser due to format or codec issues, I want to notify the user about it. When the browser is unable to play a video (because of unsupported format or codec), the onloadedmetadata event does not occur. I have some ...

How can I stop json_encode() from including the entire page in the JSON response when submitting a form to the same PHP script?

I only have a single index.php file in my project. I am aware that it's recommended to separate the logic from the view and use different files for PHP, JS, and HTML. This is just a test: <?php if($_SERVER["REQUEST_METHOD"] == "P ...

Retain the user's input in the text box even after the form has been submitted

Currently, I am tackling the challenge of creating a register form with an error handler to manage any mistakes made by users during registration. Once the form is submitted, potential errors are displayed to the user. To enhance user experience, I am ex ...