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

Establish a timeout for the Material UI drawer to close automatically after being opened

I am looking for a way to automatically close the drawer after a specific duration, but it seems that material UI drawer does not have the necessary props for this. Is there a workaround using transitionDuration or perhaps adding a setTimeout in my functio ...

Disable the "top" property for the Material UI Drawer component

I'm currently working on implementing a Persist Left Drawer from Material UI that slides in from the left side. However, I don't want the drawer to cover the entire left side of the page. Essentially, I want to remove the "top" style property men ...

What is the best way to align a div with a td within a table?

I have a table and I inserted a div inside it, but now the alignment with the td is off. I applied display: inline-block to both the td and div elements, but the td only shifted slightly. How can I correct this? Here's my HTML code for the table : ...

Tips for triggering functions when a user closes the browser or tab in Angular 9

I've exhausted all my research efforts in trying to find a solution that actually works. The problem I am facing is getting two methods from two different services to run when the browser or tab is closed. I attempted using the fetch API, which worke ...

Display Text Only When Selected - Material-UI

Is there a way to display only the selected tab's text? I came across this code snippet in the MUI documentation that involves showing labels on selection. How can I achieve this effect, maybe by manipulating the state to change the color of the selec ...

Modify input value using jQuery upon moving to the next step

When you type into an input[text] and press the tab button, it automatically moves to the next input. If you fill out all the inputs and then want to re-fill them, the values will be highlighted in blue. However, I wanted to achieve this without having to ...

Ensure that the line above is shorter in length compared to the following line

Is there a way to ensure that the previous line of text is always shorter than the next one, even if the length of the text is unknown? For example: Lorem ipsum dolor sit amet, consectetur adipiscing et libero posuere pellentesque. Vivamus quis nulla jus ...

Assigning arbitrary hidden form data from dropdown selection

Would like to assign Layers Value as one of the following: GFS Meteogram 3day std or WRF 20 Global Meteogram 3day Std depending on the option selected from the dropdown menu <div><select id="myselect" class="productViewerParameter" name=" ...

What is the best way to use Shadcn to incorporate a calendar that takes up half of my website?

Currently, I am in the process of developing a scheduling appointment system. My main challenge is getting the calendar to take up half of my page's space. Despite my attempts to adjust its height and width, I have not been successful in seeing any ch ...

Can Angular retrieve the inner HTML content of an element?

Check out this demo . In this demonstration, I have created a list of "names" and I'm trying to retrieve the text of the selected element. However, whenever I click on an item from the list, I consistently get the same "innerHTML" for all users. Is ...

Missing Cookie in request using NodeJS and NextJS

Struggling with integrating cookies in a fullstack app I'm developing using Node for backend and NextJS for frontend on separate servers. The challenge lies in getting the browser to attach the cookie received in the response header from the node serv ...

Pressing Ctrl + S enables you to preserve the webpage for future

Is there a way to save an entire webpage, including all the HTML files, using Ctrl + S in Chrome? I have tried two different methods for saving the page, but so far, neither of them has worked: from selenium import webdriver from selenium.webdriver.common ...

Guide on displaying a Custom 2D shape on both sides using three.js

As a beginner to three.js and 3D programming in general, I recently used three.js to draw a sector. However, I am facing an issue where I can only see the object in one direction but not in the opposite direction. It appears that the same phenomenon is h ...

Encountered a TypeError with mongoose: The function specified is not recognized as a valid function when attempting to create a new mongoose instance

I'm currently developing a web application using the MEAN stack, and I've encountered an issue with my mongoose instance that's giving me a headache. Let me share parts of my code: my route const express = require('express'); co ...

Using Django and Jquery to pass multiple values to HTML, with one value stored in a .js file

Here is an example of HTML code: {% for m in mortgages %} <input id ='mortgages_counter' name='mortgages_counter' type='hidden' value='{{ m.MonthlyPaid }}'> {% endfor %} If used for a selection menu: {% for ...

Is the webdriver.io waituntil method designed to return a boolean value of true or false

I am working on an automation framework using webdriver.io v5. I need to receive a boolean response from the code snippet below: waitAndCheckForContactToBePresent(contactName) { return browser.waitUntil((value) => { return this.chec ...

Exploring the Depackaging of ES6 Nested Objects

How can I implement ES6 with Destructuring to give users options? I'm having trouble dealing with nested objects and preventing the defaults from being overwritten by partial objects. Check out this simple example on MDN: function drawES6Chart({si ...

Converting Markdown to HTML using AngularJS

I'm utilizing the Contentful API to retrieve content. It comes in the form of a JSON object to my Node server, which then forwards it to my Angular frontend. This JSON object contains raw markdown text that has not been processed yet. For instance, t ...

How can I convert an HTML table into a PDF using TCPDF while ensuring that all content is contained within a single div in PHP?

I have successfully converted my JSON API data into an HTML TABLE format and now I am trying to generate a PDF from this HTML TABLE using TCPDF in PHP. Despite trying various methods related to PDF generation using TCPDF, I am facing issues with my code wh ...

Encountering the issue of "TypeError: Cannot read property 'file' of undefined" when trying to upload a video with Multer

The objective is to select a video file from the desktop and upload it to the platform. The uploaded video file will then be automatically posted to a specified Youtube channel using GCD. An error message I encountered is:"react-dom.development.js:40 ...