Learn how to display every ASCII or EBCDIC character once radio buttons have been selected

I'm currently working on a simple website that requires the user to input a minimum of 256 characters, choose between ASCII or EBCDIC for conversion, and then click the submit button (Run) to display the final converted result on the page. However, I'm facing difficulties in implementing the functionality that converts the entered text to ASCII or EBCDIC values based on the selected radio button. Below is the code snippet:

1.html:

<!DOCTYPE html>
    <html lang="en">
       <head>
          <meta charset="UTF-8">
          <title>Converter for ASCII or EBCDIC</title>
          <link href="style.css" rel="stylesheet" type="text/css"/>
          <script src="encoding.js" defer></script>
       </head>

       <body style="text-align:center;">
        <label for="text">Enter a text that is at least 256 characters long</label><br>
        <input type="text" id="text_id" name="text" minlength="256">

        <p>Select the following:</p>
        <div>
            <input id="ascii" type="radio" name="encoding" value="ascii">
            <label for="ascii">ASCII</label>
        </div>
        <div>
            <input id="ebcdic" type="radio" name="encoding" value="ebcdic">
            <label for="ebcdic">EBCDIC</label>
        </div>
        <button onclick="myFunction()" type="button">"Run!"</button>
        <label for="button">"Run!"</label>
        <p id="demo" style="color:red;">
       </body>
    </html>
  

style.css:

body 
  {
    font: 12pt Arial;   
  }

  input[type=radio] + label::before 
  {
     border-radius: 10px;   
  }
  input {
    border: 2px solid currentcolor;
  }
  input:invalid {
    border: 2px dashed red;
  }
  input:invalid:focus {
    background-image: linear-gradient(pink, lightgreen);
  }
  encod
  

encoding.js:

function myFunction() 
  {
    let str = document.getElementById("text_id");
    let a = "ASCII Code is == >  ";
    // Check if the user has selected a radio button
    let radio_selected = false;
    document.querySelectorAll('input[type="radio"]').forEach(function(radio) 
    {
      if (radio.checked) 
      {
        radio_selected = true;
      }
    })
    if (!radio_selected) 
    {
      console.log("The radio has not been checked, please select a button");
      return;
    }
    if (str.value.length >= 256) 
    {
      // Loop through the entire string entered by the user and print all converted values
      for (let i = 0; i < str.length; i++) 
      {
          document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + a + str.value.charCodeAt(i);
      }
    } 
    else if (str.value.length < 256) 
    {
      console.log("null");
      return null;
      // Print and return null if the user entered a string less than 256 characters
    }
    document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + a + str.value.charCodeAt(i);
  }
  

The input validation requirement (at least 256 characters) and radio button selection check have been implemented. However, I am still struggling with converting the entered text based on the chosen radio button and displaying all characters in ASCII or EBCDIC format.

Answer №1

To verify if the buttons have been selected, follow these steps:

function checkSelection() {

    //Retrieve both elements
    const ascii = document.getElementById('ascii')
    const ebcdic = document.getElementById('ebcdic')

    let str = document.getElementById("text_id");
    let a = "ASCII Code is == >  ";

    // Check if the input string length is less than 256 characters
    if (str.value.length < 256) {
        console.log("null");
        return null;
        // Print and return null if the string entered is less than 256 characters
    }

    // Check if a radio button has been selected
    let radio_selected = false;
    document.querySelectorAll('input[type="radio"]').forEach(function (radio) {
        if (radio.checked) {
            radio_selected = true;
        }
    })
    if (!radio_selected) {
        console.log("The radio has not been checked, please select a button");
        return;
    }
    
    // Trigger a condition based on which element is checked
    if (ascii.checked) {
        for (let i = 0; i < str.value.length; i++) {
            document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
        }
    }
    else if (ebcdic) {
        // Function to convert to EBCDIC (specific implementation required)
    }

}

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

Interested in retrieving the dynamically changing value of LocalStorage

Hopefully I can articulate my issue clearly. I am implementing a feature where CSS themes change upon button clicks. When a specific theme button is clicked, the corresponding classname is saved to LocalStorage. However, since the key and value in LocalSt ...

Ways to Update/Overwrite Current Information in HTML Using Vue

I have experience creating templates for Vue components using JavaScript. Imagine I have HTML that is generated on the server side with a language called UTL: <table> <tr> <td>[% example_var; %]</td> <t ...

Troubleshooting Issue with Post/Get Request in AJAX and Flask Framework

My current project involves building a YouTube scraper webpage purely for educational purposes. I have created a web page with a text box to enter search queries and a search button. When the button is clicked, an Ajax post request is sent with the text bo ...

Trigger is not activated by dynamically created element

I am dealing with a block of code that is dynamic and looks like this. var li3 = document.createElement('li'); li3.classList.add("col-sm-3"); li3.classList.add("no_padding"); var inner = ""; inner = inner ...

Animating numerous elements simultaneously during Vue component rendering

Take a look at this simple case in the following jsfiddle: https://jsfiddle.net/hsak2rdu/ I attempted to swap and animate two elements, but it didn't work as expected. When you click the toggle button in the playground, the second element quickly mo ...

HTML table row content should be aligned to the left side

I am attempting to align the data in the 'Address' column without any margin. I want it to start from the left since it's overflowing. You can find the HTML, CSS, and JS code here Even though I tried using <td align="left">..</td& ...

Colorful Paper Trail Logging

After stumbling upon a post on the internet, I discovered that Papertrail offers the ability to log using ANSI colors. This is particularly appealing to me as my node.js app generates numerous logs, and adding color helps me decipher the information during ...

ASP.NET service with an added quotation mark in the JSON response!

I am trying to make a web service method call in JavaScript within an ASP.NET 3.5 environment. After inspecting the result using Firebug, I found the following: {"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"} It seem ...

Tips on updating primeng Panel Menu appearance with scss

I'm looking to customize the color of my panel menu to black. Below is the HTML code I am using. <p-panelMenu styleClass="font-bold text-xs m-0 w-11" [model]="items" [multiple]='false'></p-panelMenu> ...

Managing file sizes on the client side prior to transferring them to the server side

I am looking to implement client-side validation for file size. The current code only handles success, but I also want to address file size limitations. Right now, if a file exceeds 2MB, it results in a 500 (Internal Server Error) behind the scenes. Is th ...

Utilizing Vue.js to apply conditional statements or filters on v-for generated outputs

Currently, I am working on organizing my results by category. I believe there is room for improvement in the way it's being done: <div><h2>Gloves</h2></div> <div v-for="stash in stashes" :key="stash.id"> <div v-for= ...

adjusting the background with repeat-y pattern placement

I'm having an issue with setting a background image as a wrapper for the main content of my page. Currently, I've set the background image like this: #background { background: url("../image/bg.png") repeat-y 133px 50px; color: #000000; ...

Different Approaches for Handling User Interactions in Angular Instead of Using the Deferred (Anti-?)Pattern

In the process of developing a game using Angular, I have implemented the following mechanics: An Angular service checks the game state and prompts a necessary user interaction. A mediator service creates this prompt and sends it to the relevant Angular c ...

Search through array elements that are nested deeply

Consider the following scenario: an array is provided as input containing various objects with nested elements. The goal is to filter this array in JavaScript and obtain a new array consisting only of objects where the key "navigation" has a value of true. ...

"Managing output buffers, styling with CSS, and sending emails with PHP

I am currently using a script to send emails that result in a couple of HTML tables: $from = "example.com <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82efebe7ef83898ce0ede1e9e3e6d2edeee8">[email protected]</a ...

Click event in jQuery to change the background color of <td> element

I'm still getting the hang of jQuery, so please bear with me. :) The purpose of this code is to color a square with the selected color when clicked if it's white, and turn it back to white if it's already colored. It works fine except for a ...

Struggling to get the font-weights of the Typography and Button components in the new React with Material-UI to display

In both my previous and most recent React applications, I have the following code snippets: <Typography variant="h6" color="inherit" noWrap > h6. Heading </Typography> <Button type="button" size="large" color="primary" > ...

jQuery: Issue Encountered with POST Request when Offline (iOS & Chrome)

After developing an HTML5 web application with offline capabilities using AppCache, the process flow is as follows: Online: While connected to the network, the app loads base information in advance. Offline: Users can take the app offline on their tablet ...

Troubleshooting an Issue with CSS Sliding Doors

I'm having trouble getting the right image to show on my website. The left side of the image is fine, but I can't figure out how to make the right one appear. As a HTML/CSS beginner, I'm still learning the basics. For more information, vis ...

To enhance user experience, it is recommended to reload the page once

Hello, I'm looking for a way to automatically refresh the page after submitting an AJAX form. Currently, I have an onClick function that seems to refresh the page, but I still need to press F5 to see the changes I've made. Here's the JavaSc ...