Having trouble converting a name to binary? Clicking on JS isn't producing any results? Don't worry,

Just starting out as an absolute beginner with programming and watching some tutorial videos. I've got VSC installed, but it's not displaying any errors when I run it. Any suggestions for a better program than VSC?

I'm trying to convert Input: Cipto to Output: 01100011 01101001 01110000 01110100 01101111. Any suggestions on how to do this? I need some simple code to help me understand.

function convert() {
    var input = document.getElementById("name");
    var ouput = document.getElementById("number").value;
    }
output.value = "01100011 01101001 01110000 01110100 01101111";
<html>
<head>
    <body>
        <script src = "1.js"></script>
                Type in Cipto:
                <br>
                <input type = "text" id="name";
                <br>
                <button onClick="convert();">Convert!</button>
                <br>
                <br>
                Output:
                <br>
                <input type = "number" value="number";
            

        </form>
    </body>
</head>
</html>

Answer №1

There was a typo where "ouput" was written instead of "oputput." Additionally, attempting to access a variable declared within a function from outside of its scope is not possible. Lastly, in HTML input, tags should be closed like normal tags.

function convert() {
    var input = document.getElementById("name");
    var output = document.getElementById("number");
    output.value = "01100011 01101001 01110000 01110100 01101111";
}
<html>
<head>
    <body>
        <script src = "1.js"></script>
                Type in Cipto:
                <br>
                <input type="text" id="name">
                <br>
                <button onClick="convert();">Convert!</button>
                <br>
                <br>
                Output:
                <br>
                <input type="text" id="number" value="number">
            

        </form>
    </body>
</head>
</html>

While this code will function, it will always input the same output in the input field. To convert every input to its binary form, the convert function should be written differently as shown below:

function convert() {
  var input = document.getElementById("name").value;
  var output = document.getElementById("number");
  output.value = "";
  for (var letter of input) {
      output.value += letter.charCodeAt(0).toString(2) + " ";
  }
}
<html>
<head>
    <body>
        <script src = "1.js"></script>
                Type in Cipto:
                <br>
                <input type="text" id="name">
                <br>
                <button onClick="convert();">Convert!</button>
                <br>
                <br>
                Output:
                <br>
                <input type="text" id="number">
            

        </form>
    </body>
</head>
</html>

Answer №2

I'm still learning my way around here, so please forgive me if I make a mistake, but I noticed errors in both the html and js code you provided. I made some changes to the html:

<html>
    <head>
        <body>
            <script src="1.js"></script>
            Type in your name:
            <br>
            <input type="text" id="name">
            <br>
            <button onClick="convert();">Convert!</button>
            <br>
            <br>
            Output:
            <br>
            <input id="binary-output">
        </body>
    </head>
</html>

As for the JavaScript:

function convert() {
    var input = document.getElementById("name").value;
    var output = document.getElementById("binary-output");
    output.value = "";
    for(var i = 0; i < input.length; i++) {
        output.value += input[i].charCodeAt(0).toString(2) + " ";
    }
}

In the html, I made changes to the input forms by simplifying them and adding ids. In the js function, I converted each character in the input string to its binary ascii value. That's all there is to it! I hope this explanation clears things up.

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

Accessing the Angular scope and making modifications using Chrome browser

I need to access the $scope value in order to update its data values via a chrome extension. I have attempted to obtain the $scope using the code below: var $scope = angular.element(document.getElementById('name')).scope() While this code wor ...

Node.js: The choice between returning the original Promise or creating a new Promise instance

Currently, I am in the process of refactoring a codebase that heavily relies on Promises. One approach I am considering is replacing the new Promise declaration with simply returning the initial Promise instead. However, I want to ensure that I am correctl ...

Eliminate Tracking Parameters from URL

I rely on UTM parameters to monitor incoming links within Google Analytics. Consider a scenario where my URL appears as follows https://www.example.com/store?utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale I am looking to streaml ...

What is the method to deactivate multiple links using jQuery?

Below is the HTML code: <a title="Login" data-href="/MyAccount/Access/Login" data-title="Admin" data-entity="n/a" id="loginLink" class="nav-button dialogLink"><b>Login</b></a> <a title="Register" data-href="/MyAccou ...

Display an icon from the glyphicon library in an Angular application based on a

My Controller: .controller('BlogController', function(blogFactory, $routeParams, $scope){ var that=this; stat=false; this.checkbookmark = function(bId){ console.log(bId) blogFactory.checkBookmark(bId, function(response){ ...

What is the best way to position my header at the top of my navigation bar?

I am new to the world of HTML and CSS! My goal is as follows: https://i.stack.imgur.com/hmLNS.png This is my progress so far: https://i.stack.imgur.com/rav8P.png I am also looking to have the header fill the entire browser window and remain fixed, wit ...

Struggling to make even the most basic example work with TypeScript and npm modules

After stumbling upon this repository that made using npm modules within a Typescript program look easy, I decided to give it a try by forking it and making some changes. My goal was to add another package to get a better understanding of the process. So, I ...

Angular JS model not being updated when selecting a date value with datepicker

Whenever I try to use the datepicker, I encounter an issue where the model value doesn't bind to the model when I select a date. Nothing happens when I select a date. Can anyone point out where I might be going wrong? Any help would be greatly appreci ...

What is the simplest way to extract only the error message?

Having this code snippet. $('div#create_result').text(XMLHttpRequest.responseText); If we look at the content of XMLHttpRequest, it shows: responseText: Content-Type: application/json; charset=utf-8 {"error" : "User sdf doesn't exist"} st ...

If you want to retrieve the calculated value of a div using jQuery

I have a scenario where I have 3 list items (li) under an unordered list (ul). I am interested in finding the height of these list items, but without explicitly defining their height. So far, when inspecting with Firebug, I noticed that the computed height ...

MongoDB does not recognize Db.Collection as a valid function

A lot of people have been inquiring about this specific error, but after thorough investigation, I couldn't pinpoint a similar issue. So I'm reaching out in the hopes that someone might be able to identify and help rectify it. Let me provide som ...

Text area capacity

Is there a limit to the maximum capacity of a textarea for accepting text? The HTML page functions correctly when the text is limited to around 130-140 words. However, if the text exceeds this limit, it causes the page to hang without any response. The tex ...

Exploring the wonders of accessing POST request body in an Express server using TypeScript and Webpack

I am currently working on a Node and Express web server setup that utilizes Webpack, along with babel-loader and ts-loader. Let's take a look at some key portions of the code: webpack-config.js: const path = require("path"); const nodeExte ...

Is there a way to create more space between the cards in my project?

Could someone help me with separating the cards in my HTML project? I'm new to coding and struggling with this. The cards are too close together for my liking, is there a way to adjust the spacing? Below is the code I currently have: * { box-sizin ...

The useTransition() method in React remains stuck in the isPending state when making API calls from routes in the /pages/api directory

I'm encountering an issue with the useTransition() function where it remains true and never changes back to false. I am attempting to delete a record from MongoDB and after completion, I want to refresh the React Server Component following the guideli ...

Is it possible to hide a portion of a jQuery UI draggable element using display:none while maintaining the cursor position?

My challenge involves having draggable elements with images that need to be dropped into folders. In order to maximize screen space and keep more draggables visible at once, I have decided to hide the images using CSS during the drag operation. However, I ...

The appearance of the check box remains stagnant visually

Having trouble with dynamically changing the state of a checkbox based on a database value. Even though the value changes after a button click, the visual state of the checkbox remains the same. Here is the link to the JSFiddle for testing: http://jsfiddle ...

iOS devices are experiencing issues with touchstart and touchend events not functioning when utilizing jquery mobile and cordova

During a previous project, I encountered no problems with the following code snippet. It effectively utilized touchstart and touchend events to adjust the CSS of a button: <script> $('input[type="button"]').on('touchstart', func ...

Issue with Bootstrap 4 navbar dropdown on mobile: unable to click on menu item

My website is currently in development with Bootstrap 4. I'm facing an issue where, on mobile devices, when the menu items collapse into the three bars, they are not clickable. Despite following the recommendations in the Bootstrap documentation, the ...

Execute the component function located within one page using another page

Can someone help me understand how to trigger OnSwipe from inside a different JS file named CardFooter.js? This file contains a button with an OnClick event that triggers OnSwipe from the preceding JS file called CardItem.js. Both of these files are includ ...