Creating Functions with HTML, CSS, and Javascript

I have been working on a website that is designed to convert numbers for you. Most of the code is complete, but I have encountered an error that I am currently unable to resolve. When I try to run the code, I see content on the page but when I click the button, the conversion does not occur as expected.

<div id = "conversion">
function feet() {
  var userinches = document.getElementById('userinches').value;
  doOuput('The answer is', userinches / 0.0833, ' ft');
}

function doOuput(val, unit) {
  //console.log('Convert To ' + unit);
  document.getElementById('results').innerHTML = val + unit;
}
</div><!-- End of conversion div section -->

Answer №1

To properly incorporate JavaScript into your code, make sure to enclose it within a script tag. It's recommended to separate your JavaScript code from the HTML file by creating a separate JS file and then including that file in your HTML document.

<div id="question">
  <h2>Unit Convertor</h2>
  <img src="" alt="Picture of basketball">
  <p>Enter a Quanity of Inches: <input id="userinches" type="number" /> </p>
</div>

<div id="conversion">
  <script>
    function feet() {
      var userinches = document.getElementById('userinches').value;
      doOuput('The answer is', userinches / 0.0833, ' ft');
    }

    function cm() {
      var userinches = document.getElementById('userinches').value;
      doOuput('The answer is', userinches * 2.54, ' cm');
    }

    function yards() {
      var userinches = document.getElementById('userinches').value;
      doOuput('The answer is', userinches / 0.02778, ' yard');
    }

    function doOuput(val, unit) {
      //console.log('Convert To ' + unit);
      document.getElementById('results').innerHTML = val + unit;
    }
  </script>
</div>

<div="buttons">
  <button type="button" onclick="feet();">Feet</button>
  <button type="button" onclick="cm();">Centimeters</button>
  <button type="button" onclick="yards();">Yards</button>
  </div>
  <!-- End of buttons div section -->

  <div id="results"></div>

Answer №2

Ensure Javascript is implemented within the <script> tag. It is recommended to utilize an external file as demonstrated below:

<script src="myscripts.js"></script>

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

Displaying a background image in Laravel using the storage directory

Can anyone offer some guidance on how to load a background image from storage? I have tried using the following code with normal img src, but it doesn't seem to work with background images. background-image: url({{ Storage::get("{$post->image}") } ...

Having difficulty receiving a response from an AJAX call within the success function

After browsing through this stack link Ajax response not calling success:function() when using jQuery 1.8.3, I'm puzzled as to why the success function is not invoked when I uncomment the dataType line. It seems that setting dataType = JSON prevents t ...

Is it possible to identify images within a message sent by users to the server and provide a response accordingly

Apologies for my not-so-great English I am currently learning JavaScript and I am trying to detect an image in a message sent by users from the server, and reply with that image embedded in a bot message. However, message.content is not working for this p ...

Leveraging PHP variables to determine the image location, implement an onclick function to cycle through the image gallery

I am facing an issue with a database query that is supposed to display multiple images. These images are stored in a PHP variable and shown one at a time using a JavaScript gallery with an onclick function to navigate through them. Unfortunately, the funct ...

Troubleshooting the ThreeJs THREE.Mesh position problem

As a newcomer to three.js, I am exploring the three.js libraries and trying to understand how they work. I have successfully rendered a sphere on the screen, but I am having trouble figuring out what exactly the .position member of the sphere is referencin ...

Loop through an array of objects in Node.js using ng-repeat

I have integrated angularJS with a node back-end that transmits data using socketio. When I attempt to display the data using ng-repeat, I encounter an issue. If I set the initial data within the controller, ng-repeat functions properly. However, if I add ...

Unable to successfully change the Span Class

My webpage has the code snippet below, but it's not functioning as expected. I have tried two methods to change the span's class attribute, but they aren't working. Could someone please help me identify where the issue lies? :) <script l ...

Using Twitter bootstrap with Node.js and Express framework

Is it possible to integrate Twitter Bootstrap with Node.js and Express? I understand that I need to place CSS and Javascript files in the ./public directory (if it's set as default). However, when trying to implement Twitter Bootstrap on Node.js and ...

connect to new database using mysqli_find_cat

<?php $host = "localhost"; $username = "root"; $password = ""; $db = "mineforums"; $connect = mysqli_connect($host, $username, $password, $db) or die(mysqli_error($connect)); ?> Here is the PHP snippet that connects to my databa ...

Create a unique V-MODEL for each index that I generate

How can I customize the V-MODEL for each object generated? I am developing a cupcake website where users can create multiple forms with just one submission. However, when generating two fields, the inputs of the second field are linked to the inputs of t ...

Steps to incorporate / insert Angular directive in an application

In my app, the main.js file is located in the root folder. -app |_ routes.js |_ main.js -components |_directives |_abc-directive.js I am trying to figure out how to define a directive that can be accessed from a different folder. This is what I at ...

I am currently experiencing some challenges with using jquery ajax post in conjunction with PHP

I am facing an issue with jQuery AJAX post and PHP. My attempt is to send data using .serialize() along with additional data. However, in PHP, I can only receive the data from .serialize() but not the additional data. In my jQuery Ajax code, f ...

Guide on assigning a callback function in JavaScript

In this code snippet, I am initializing a new object variable and passing an object as an argument: const newObj = new customObject({ first : $('#fname').val(), last : $('#lname').val(), fn : function() { alert(this ...

In pursuit of increased speed

When using $routeProvider in Angular, I have noticed that every time I navigate to a specific route, I see the following logs in the console: XHR finished loading: "http://localhost:8080/root/partials/view1.html". XHR finished loading: "http://localhost:8 ...

php utilizing javascript to generate encrypted data for a hidden file

Within my MVC application, I have implemented Raty for rating images. Below is the code snippet: <div class="container"> <form method="post" class='form' role='form' action="?section=photo&view=addVote"> <input t ...

Manipulating Angular and Typescript to utilize the method's parameter value as a JavaScript object's value

I am currently working with Ionic, Angular, and Typescript, attempting to dynamically set the value of a location based on the parameter passed from a method. Here is the relevant code snippet: async fileWrite(location) { try { const result = a ...

Tips for incorporating AngularJS into a .php file when displaying a Laravel view

I am completely new to working with laravel and AngularJS. Currently, I am facing an issue where I am trying to display a view that is a php file. Although the .php file is successfully displayed, the AngularJS expression within it is not being interpreted ...

Update the content of a div and refresh it when a key on the keyboard is pressed

I need to hide the images in a div when I press a key on the keyboard. How can I achieve this? <div> <span role="checkbox" aria-checked="true" tabindex="0"> <img src="checked.gif" role="presentation" alt="" /> ...

Which is better for creating hover effects: CSS3 or JavaScript?

When hovering over a link, I want to highlight a specific picture and blur the rest. Here's my HTML code: <body> <div id="back"> <div id="one"></div> <div id="two"></div> </div> ...

Personalizing buttons on a carousel in React-bootstrap

I am facing an issue with my carousel and buttons placement. The buttons need to be outside of the carousel, but I'm unsure how to connect them effectively. React-Bootstrap's activeIndex option was suggested, but since my carousel is not cyclic l ...