"Troubleshooting: Text Added to Element in JavaScript Not Appearing

I am in the process of developing an online task list for users, and I want it to work in such a way that when a user enters a task and clicks the 'JotIT' button, the task is added to the list.

Currently, the function to add tasks is working, but the text is not visible for some reason, even though it appears in the HTML when inspected.

<script>
    var welcome = 'Welcome To JotIT';

    var jotItem = function()
        {
            //Create & Get the necessary elements
            var item = document.createElement('input'),
            space = document.createElement('br'),
            form = document.getElementById("listing"),
            frag = document.createDocumentFragment(),
            textVal = document.getElementById("input-jot");

            //Set Attributes to list item
            item.setAttribute('type', 'checkbox');
            item.setAttribute('name', 'jot-list');

            //If there is no input in the textbox then create an alert popup
            if(textVal.value === "")
                alert("Please insert a value");
            else {
                item.innerHTML = textVal.value;
                frag.appendChild(item);
                frag.appendChild(space);
                form.appendChild(frag);
                textVal.value = ""; 
            }
        }
</script>

</head>
  <body>
    <h1 id="head">JotIT</h1><br>
    <p> <script type="text/javascript">document.write(welcome);</script></p>
    <input type="form" id= "input-jot">
    <button class = 'btn-info' id="jot-down" onclick=jotItem();>JotIt!</button>
    <!-- TODO: Add form submit tag instead of ul tag -->
    <!-- TODO: Align the list items -->
    <form id = "listing" method="get">
        <input type="checkbox" name="me"> Start </input>

    </form>

  </body>

Answer №1

Remember to include a label when inserting inputs, as they should be self-closing or empty elements. For checkboxes, use the label element for proper display instead.

    var welcome = 'Welcome To JotIT';

    var jotItem = function() {
      //Create & Get the necessary elements
      var item = document.createElement('input'),
        label = document.createElement('label'),
        space = document.createElement('br'),
        form = document.getElementById("listing"),
        frag = document.createDocumentFragment(),
        textVal = document.getElementById("input-jot");

      //Set Attributes to list item
      item.setAttribute('type', 'checkbox');
      item.setAttribute('name', 'jot-list');

      //If there is no input in the textbox then create an alert popup
      if (textVal.value === "")
        alert("Please insert a value");
      else {
        label.innerText = textVal.value;
        frag.appendChild(label);  // this is where the label goes
        frag.appendChild(item);
        frag.appendChild(space);
        form.appendChild(frag);
        textVal.value = "";
      }
    }
</head>

<body>
  <h1 id="head">JotIT</h1>
  <br>

  <input type="form" id="input-jot">
  <button class='btn-info' id="jot-down" onclick=jotItem();>JotIt!</button>
  <!-- TODO: Add form submit tag instead of ul tag -->
  <!-- TODO: Align the list items -->
  <form id="listing" method="get">
    <label>Start</label>
    <input type="checkbox" name="me" />

  </form>

</body>

Answer №2

The reason for the confusion lies in your incorrect method of adding the label to the input field. By using the following HTML syntax, you will be able to successfully address this problem:

<label><input type="radio" name="checklist-item" />The Label Text</label>

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

Cookie vanished post registration in the captive portal

Just a heads up, I'm new to this. I've encountered an issue where a cookie doesn't persist after captive portal login. The setup involves landing on our web server, storing MAC and a unique ID on two cookies, redirecting for authentication, ...

What is the process of embedding audio in HTML5 using base64 encoding?

I have a base64 encoded audio record in WAV format (data link - {{vm.record}}). I am trying to add an audio player to a widget written in JS and HTML that can play the audio for me. I'm not sure why it's not working. Do I need to write something ...

Transmitting boolean values from a Python API request

Is it possible that the error in my script is due to the boolean value I am trying to send in an API put call using Requests in Python? Here is how my script looks: import requests data = { 'name': 'John', 'lastname': ...

Using AngularJS to Nest ng-view within ng-repeat

I have a collection of items. Each item has buttons to display its details and comments within this ng-view section. It is necessary for the user to view all available product details on one page, for example. Below is the HTML list: <div ng-controll ...

angular interpolation:issue arises when inserting URL stored in a variable

A challenge I'm facing involves adding a dynamic id to a YouTube URL, which looks something like this: <iframe width="460px" height="415px" ng-src="{{post.youtube_id}}" frameborder="0" allowfullscreen></iframe> One of the URLs I'm a ...

When I integrate the Google map on my website, it appears to be fully zoomed out by default. However, if you access the map directly through the link provided, it

After creating a custom public map on google.maps, I noticed that the embedded version on my site is zoomed out too far and not centered on my location like the original. You can view it here. Here's the code I'm using to embed the map: <ifr ...

Adjusting the height of a div inside a Material-UI Grid using Styled Components

When the mouse hovers over the two cells highlighted in pink, they turn pink. Is there a way to make the entire grid fill with pink when hovered over, both width and height at 100%? Check out the sandbox here ...

Enable vertical scrolling within the table body to display entire rows

Encountering an issue with using overflow-y inside tbody. Attempting to set the maximum height of the tbody results in it shrinking into a single column, as shown in image 1. Seeking assistance in resolving this problem. Thank you. Below is the HTML code: ...

The writeToDB function is triggered only one time

I've encountered an issue with my node.js code where the writeToDB function is being called, but data is not inserted in the database after the first time. Can someone help me understand why? uploadInfoObject = { ProcessId: pid, Type: "type", ...

What is the best way to incorporate a dynamic background image into a panel heading using Twitter Bootstrap 3 for a responsive design?

Hey everyone! I'm having trouble adding an image as a background to my panel heading and also including an h3 inside it. Below is the code I am using: <div class="row col-wrap"> <div class="col-lg-4 col"> < ...

Cancel the starting path in Angular 9 and initiate a redirection

I am attempting to change the initial path such as localhost:4200/ so that it displays an error view instead of the home page. I want the home page to only be accessible if you navigate to something like localhost:4200/tag1/tag2. I need to be able to capt ...

What sets PHP fsockopen apart from WebSockets in HTML5?

Are there differences between server-side and client-side processing? Is it possible to send instant messages using PHP's fsockopen, or am I restricted to using JavaScript for polling on the client side? ...

Tips for ensuring that jQuery runs in the correct order within Angular 2

As a beginner with Angular 2 and asynchronous loading, I have encountered some difficulties in finding the right approach for running jQuery scripts in the correct sequence within an Angular 2 project. Despite exploring various resources such as: How to u ...

Experiencing issues with implementing shopping cart logic using KnockoutJS. Need help

The Objective Create a dynamic list of products. The Scenario Overview In my online shopping application, I want to showcase the products I add to my shopping list in a sidebar when I click the 'add button' for each product. Brief Problem Sum ...

Does anyone have an idea of the origin of the item in this ajax .each function?

Currently, I am utilizing the Etsy API with JavaScript by calling this AJAX code: $.ajax({ url: etsyURL, dataType: 'jsonp', success: function(data) { This code returns an object array, if I'm not mistaken. It then proceeds to enter this . ...

Modify the hover color of the FontAwesome span icon

Here's a span that I have: <span class="fa fa-green fa-arrow-circle-o-right" aria-hidden="true"></span> I decided to change the color to #008d4c like this: .fa-green { color: #008d4c; } But when I try to hover over it, the color do ...

When attempting to upload a file using ajax, the $_FILES variable in PHP came

I am facing an issue with uploading images via AJAX where my PHP is not receiving the AJAX post. function handleFileSelect(evt) { files = evt.target.files; // FileList object $('.thumb-canvas' + filesId).css('display','bl ...

Looping through PHP code that generates the same ID twice in a random order

I am currently working on developing a memory game and have encountered an issue. I need to loop through all the images with unique ids (2x id1, 2x id2, 2x id3, etc.) and assign random ids to each image. Here is the PHP code snippet: class Kaart { pu ...

How can you customize the appearance of the filledInput component within a TextField component in Material UI?

I need some guidance on how to change the color of the FilledInput component within a TextField. Unlike InputProps, FilledInputProps are not directly accessible for styling with classes. Any suggestions on how I can customize the styling of the FilledInpu ...

Is there a type-safe alternative to the setTimeout function that I can use?

When running my tests, I encountered an issue with the setTimeout method making them run slower than desired. I initially attempted to address this by using "any" in my code... but that led to complaints from eslint and others. Now, I have implemented a ...