The error remains a mystery to the console - could it be possibly linked to the onclick() method?

Despite checking thoroughly, the console remains empty. I came across this code in a video tutorial where it worked perfectly.

Below is the simple code that I am currently using:

function addItem() {
  inames = []
  iqtyp = []
  iprice = []
  inames.push(document.getElementById('pname').value);
  iqtyp.push(parseInt(document.getElementById('pqty').value));
  iprice.push(parseInt(document.getElementById('price').value));

  displayCart();
}

function displayCart() {
  cartdata = '<table><tr><th>Product Name</th><th>Quantity</th><th>Price</th><th>Total</th></tr>';

  total = 0;

  for (i = 0; i < inames.length; i++) {
    total += iqtyp[i] * iprice[i]
    cartdata += "<tr><td>" + inames[i] + "</td><td>" + iqtyp[i] + "</td><td>" + iprice[i] + "</td><td>" + iqtyp[i] * iprice[i] + "</td><td><button onclick='delElement(" + i + ")' >Delete</button></td></tr>"
  }

  cartdata += '<tr><td></td><td></td><td></td><td>' + total + '</td></tr></table>'

  document.getElementById('cart').innerHTL = cartdata;
}

function delElement(a) {
  inames.splice(a, 1);
  iqtyp.splice(a, 1)
  iprice.splice(a, 1)
  displayCart()
}
#frm {
  padding: 20px;
  border: 2px solid;
}

#cart {
  margin-top: 30px;
  padding: 20px;
  border: 2px solid;
  background: lightgreen;
  z-index: -100;
}

th,
td,
tr {
  border: 1px solid;
}
<!doctype html>
<html lang="de">

<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width">
  <title> Cinematic </title>
  <script src="shoppingcart.js"></script>
  <link href="shoppingcart.css" rel="stylesheet" type="text/css" />
</head>

<body>

  <div id='frm'>
    <label>Product Name</label>
    <input id='pname' type='text'><br>
    <label>Quantity</label>
    <input id='pqty' type='number'><br>
    <label>Unit Price</label>
    <input id='price' type='number'><br>
    <button onclick="addItem()"> Add Item </button>
  </div>

  <div id='cart'></div>

</body>

</html>

No output from the console is appearing, leaving me puzzled.

Answer №1

There seems to be a typo in your code where you did not properly write HTML when using .innerHTML on the last line of the function displayCart()

function addItem() {
  inames = []
  iqtyp = []
  iprice = []
  inames.push(document.getElementById('pname').value);
  iqtyp.push(parseInt(document.getElementById('pqty').value));
  iprice.push(parseInt(document.getElementById('price').value));

  displayCart();
}

function displayCart() {
  cartdata = '<table><tr><th>Product Name</th><th>Quantity</th><th>Price</th><th>Total</th></tr>';

  total = 0;

  for (i = 0; i < inames.length; i++) {
    total += iqtyp[i] * iprice[i]
    cartdata += "<tr><td>" + inames[i] + "</td><td>" + iqtyp[i] + "</td><td>" + iprice[i] + "</td><td>" + iqtyp[i] * iprice[i] + "</td><td><button onclick='delElement(" + i + ")' >Delete</button></td></tr>"
  }

  cartdata += '<tr><td></td><td></td><td></td><td>' + total + '</td></tr></table>'

  document.getElementById('cart').innerHTML = cartdata;
}

function delElement(a) {
  inames.splice(a, 1);
  iqtyp.splice(a, 1)
  iprice.splice(a, 1)
  displayCart()
}
#frm {
  padding: 20px;
  border: 2px solid;
}

#cart {
  margin-top: 30px;
  padding: 20px;
  border: 2px solid;
  background: lightgreen;
  z-index: -100;
}

th,
td,
tr {
  border: 1px solid;
}
<div id='frm'>
  <label>Product Name</label>
  <input id='pname' type='text'><br>
  <label>Quantity</label>
  <input id='pqty' type='number'><br>
  <label>Unit Price</label>
  <input id='price' type='number'><br>
  <button onclick="addItem()"> Add Item </button>
</div>

<div id='cart'></div>

Answer №2

Could you kindly update the line in the Code?

document.getElementById('cart').innerHTL = cartdata;

with

document.getElementById('cart').innerHTML = cartdata;

it's important to note that there is no attribute called innerHTL

Answer №3

There appears to be a mistake in your displayCart function. The HTML is not spelled correctly; it should read as follows:

document.getElementById('cart').innerHTML = cartdata;

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

Guide on accessing a method from a div element in VueJS

Is there a way to only render a div based on a specific condition and call a method when the condition is true? Here's an example of what I'm trying to achieve: <div v-if="timetable === null" @click="mymethodhere"> ...

Display the dropdown selection

I am trying to customize my dropdown menu so that when I hover over the main menu item (Sample Page), only the About submenu is displayed, not all of the dropdown menus. li.menu-item.dropdown .dropdown-menu { position: absolute; top: 70px; visibil ...

Exploring the use of the Next.js page router for implementing internationalization (i18

I need assistance implementing Dutch and English language translations for my entire website. Can anyone provide guidance? I have tried using i18n localizely, but it only works for individual pages. I am looking to implement translations on a larger scale ...

Countdown Widget - Transform "0 Days" to "Today" with jQuery

I am currently working on a countdown page using the Keith Woods Countdown jQuery plugin which can be found at . Here is the code I have implemented so far: $(document).ready(function () { var segera = new Date(); segera = new Date(segera.getFullYear(), 2 ...

Identify the fields in the form that have been edited for easier reference

Seeking guidance on creating a form with interactive input box fields. When a user inputs something, the field highlights in orange. Moving to another field removes the highlight and focuses the new box. Clicking "save" stores the form data. Pressing "mo ...

Delay fading in during the loading process

Recently, I came across a neat animation effect that I would love to incorporate into an upcoming project. The effect involves animating the opacity on load, also known as a fade in. I am curious if it is possible to link multiple elements together (for ...

How come only the individual top, bottom, left and right paddings function correctly while the combined padding does not respond?

Seeking to customize a button using SCSS file that is connected to HTML. This button is part of a flex layout design. The element's size is set at 83.333×16px. The box-sizing property is defined as: box-sizing: border-box; Adding padding with s ...

Issues with implementing Bootstrap tabs in Vue application

Currently, I am in the process of developing an application using vue, vue-router, and bootstrap. My goal is to integrate tags in bootstrap as shown below: <ul class="nav nav-tabs" role="tablist"> <li class="nav-item"> <a class="nav-l ...

Error message: `$.each` is not a valid function in ReactJS

I am facing an issue with my ReactJS (Create-React-App) project. It renders successfully on localhost when I run the command npm start, but encounters an error when I run the build command: npm run build. yarn run v1.16.0 $ react-scripts build Creating an ...

Show the particular entry to be viewed upon clicking the link provided in the email

How can I create a link in an email that, when clicked, displays data for a single record? Currently, I have a datatable on the index page that shows all records when the app is opened. When a change is made to a record, an email is sent out with the ID o ...

Cordova does not support the transform property

In the process of creating an Apache Cordova app using the Ionic framework and working with Phonegap Build, I encountered a challenge. The main page of my app features rows of icons that appear like this: https://i.sstatic.net/7LE7r.png However, there is ...

Having trouble confirming signature with Node.js Crypto library, specifically with key pairs

I have a concise nodejs code snippet where I attempt to sign a string and then verify it using node crypto along with key pairs generated through openssl. Despite trying various methods, the result consistently shows "false" indicating that the signature c ...

Changing images on hover with jquery

Hello, I've been working on this issue for quite some time now and I could really use some help. I am new to javascript and jquery and currently facing a challenge. I have two images - one black and the other blue. There are 4 black images that should ...

What is the best way to manipulate the positioning and rotation of the main object?

https://i.sstatic.net/HSRZz.png https://i.sstatic.net/1dggf.png After developing a toolbox capable of rotating and repositioning a mesh with an axis in the middle, I ran into an issue where separating rotation and position caused the position to revert to ...

Identify the ANGLE using JavaScript

I'm experiencing an issue with my WebGL / Three.js game where there is an unhelpful shader program linking error when ANGLE is used for providing WebGL. To address this, I am looking to implement a prominent warning specifically for ANGLE users on the ...

Restricting the type of user input in HTML forms

Requirements: Input must be a whole number between 2 and 99, inclusive. Current Solution: <input required type="number" min="2" max="99" oninput="this.value = Math.abs(this.value)" maxLength="2" matInp ...

How can I efficiently load AJAX JSON data into HTML elements using jQuery with minimal code?

I have successfully implemented a script that loads an AJAX file using $.getJSON and inserts the data into 2 html tags. Now, I want to expand the JSON file and update 30 different tags with various data. Each tag Id corresponds to the key in the JSON strin ...

Jquery deactivates the CSS hover effect

Having a dilemma with linked photos on my website. I have set their opacity to 0.45 by default, but want them to change to full opacity (1) when hovered over or clicked. I've implemented a JQuery function that resets the rest of the photos back to 0.4 ...

Restricting buffer size in HTML5 video for an MP4 file

Can the HTML5 Video tag be utilized to load an MP4 file while managing the amount of data downloaded/buffered from the server (assuming byte ranges are supported)? In essence, I would like to specify "only download 60 seconds ahead" in order to prevent th ...

What is the best way to keep a div element fixed on the page no matter where you scroll?

For example: I have an extensive inventory of items, and as I hover my mouse over each item, a corresponding picture appears in this section. The div stays fixed in place regardless of scrolling. It works similar to a frame. ...