The function element.innerHTML is invalid when trying to assign an object value as an option

Hey there! I'm currently working on a JavaScript project where I have an input that retrieves text from an array. Each option in the array needs to be linked to an object so I can utilize its attributes.

const data = [
  {
    name: "SIMPLES NACIONAL – MEI",
    funcionarioIncrease: 49.99,
    socioIncrease: 0,
    FATURAMENTO: [
      {
        name: "ATÉ 30.000,00",
        value: 49.99,
      },
      {
        name: "De 30.001,00 a 50.000,00 ",
        value: 99.99,
      },
    ],
  },
  {
    name: "SIMPLES NACIONAL – SERVIÇOS",
    funcionarioIncrease: 25,
    socioIncrease: 25,
    FATURAMENTO: [
      {
        name: "ATÉ 30.000,00",
        value: 149.99,
      },
      {
        name: "De 30.001,00 a 50.000,00 ",
        value: 199.99,
      },
    ],
  },
];

const Modes = () => {
  if (data instanceof Array) {
    return data.map((value) => {
      return {
        name: value.name,
        funcionarioIncrease: value.funcionarioIncrease,
        socioIncrease: value.socioIncrease,
        faturamento: value.FATURAMENTO,
      };
    });
  } else {
    return null;
  }
};

let results = function () {
  const modes = Modes();
  
  let selectHeader = document.querySelectorAll(".select__header");
  let selectItem = document.querySelectorAll(".select__item");

  modes.map((value) => {
    let element = document.createElement("div");
    element.classList.add("select__item");
    element.innerHTML(value.name);
  });

  selectHeader.forEach((item) => {
    item.addEventListener("click", selectToggle);
  });
  selectItem.forEach((item) => {
    item.addEventListener("click", selectChoose);
  });

  function selectToggle() {
    this.parentElement.classList.toggle("is-active");
  }

  function selectChoose() {
    let text = this.innerText,
      select = this.closest(".select"),
      currentText = select.querySelector(".select__current");
    currentText.innerText = text;
    select.classList.remove("is-active");
  }
};

results();
.select {
  position: relative;
  width: 100%;
}
.select.is-active .select__body {
  display: block;
}

.select__header {
  border: 1px solid #ccc;
  cursor: pointer;
  display: flex;
}
.select__current {
  font-size: 18px;
  line-height: 24px;
  padding: 8px;
}
.select__icon {
  align-items: center;
  display: flex;
  flex-shrink: 0;
  justify-content: center;
  height: 40px;
  margin-left: auto;
  text-align: center;
  width: 40px;
}

.select__body {
  border: 1px solid #cccccc;
  border-top: 0;
  display: none;
  left: 0;
  position: absolute;
  right: 0;
  top: 100%;
}
.select__item {
  cursor: pointer;
  font-size: 16px;
  line-height: 24px;
  padding: 8px;
}

.select__item:hover {
  background-color: #f2f2f2;
}
          <div class="service_mode flex">
            <div class="select is-active">
              <div class="select__header">
                <span class="select__current">Value 1</span>
                <div class="select__icon">&times;</div>
              </div>
              <div class="select__body"></div>
            </div>
          </div>

I seem to be facing difficulty in mapping my array and adding its inner HTML as the attribute name of my object. Additionally, I am struggling to link the options to their respective objects.

Answer №1

When encountering the error message

element.innerHTML is not a function
, remember to replace element.innerHTML(value.name); with element.innerHTML = value.name;

Here's how your updated code should look:

modes.map((value) => {
    let element = document.createElement("div");
    element.classList.add("select__item");
    element.innerHTML = value.name;
});

Answer №2

Check out the code snippet below:

element.innerHTML = value.name;

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

Can double curly braces be added within an HTML attribute when using Vue?

Suppose I would like to include <input value='{{default}}'></input> in regular HTML. This will display a textbox with {{default}} as the default input. However, when attempting to achieve the same thing with Vue, it does not work as ...

Clearing text fields in jQuery by cloning them

I am attempting to duplicate the friend fields (name + email) when the "add another friend" button is clicked. However, the cloned fields end up including the text inside them, which is not desired! Does anyone have any suggestions or solutions? Here is t ...

Can you explain the distinctions among <Div>, <StyledDiv>, and <Box sx={...}> within the MUI framework?

When exploring the MUI documentation, I came across a table of benchmark cases that can be found here. However, the differences between the various cases are not clear to me. Can someone please explain these variances with real examples for the following: ...

Sending a document through the input field with React Hook Form

In my application, I have implemented a file input to submit a file and send it to a firebase storage bucket. To achieve this functionality, I am utilizing the react-hook-form library. However, I encountered an issue - I wanted the file to be uploaded with ...

Leveraging the XMLHTTP object to extract data from websites with VBA

I am currently working on extracting the "key people" information from a Wikipedia page: https://en.wikipedia.org/wiki/Abbott_Laboratories and inserting that data into my Excel spreadsheet. I have successfully achieved this using xml http, which I find ef ...

Exploring the methods for monitoring multiple UDP ports on a single address in Node.js within a single process

I am currently working on developing a Node.js application to manage a small drone. The SDK provides the following instructions: To establish a connection between the Tello and a PC, Mac, or mobile device, use Wi-Fi. Sending Commands & Receiving Responses ...

Clicking the button will trigger the onclick event

I'm working on a button component in TypeScript and I have encountered an issue with passing the event to the submitButton function. import * as React from 'react'; interface Props { className?: string; text: string; onClick?(event: Reac ...

Tips on capturing a URL using JQuery

Currently, I am in the process of importing an external HTML file into my webpage. Within this file, there is a JavaScript method linked to a form submission event. function ValidateInput(){ //some code if(validationFailed){ ...

Integrating a PHP function within an ECHO statement

Here is a code snippet I'm currently using in my Wordpress: function wpstudio_doctype() { $content = '<!DOCTYPE html>' . "\n"; $content .= '<html ' . language_attributes() . '>'; echo apply_filte ...

Identifying the conclusion of a folder being dropped in vanilla JavaScript

I am working on determining when a directory tree has been completely traversed after being dropped onto a page. My goal is to identify the point at which the next process can begin once the entire folder and its sub-directories have been processed by the ...

Exploring shader file content within three.js with the help of jQuery

I am trying to retrieve the content of a string that I have imported using HTML from within another script. To include the text file in question in the html file: <script src="shaders/fragmentshader.fs" id=fragmentshader></script> After impo ...

When running the PHP script, the output is shown in the console rather than in the

Here is a PHP script snippet that I am working with: <?php add_action('wp_ajax_nopriv_getuser', 'getuser'); add_action('wp_ajax_getuser', 'getuser'); function getuser($str) { global $wpdb; if(!wp_verif ...

What could be causing my ajax post function to malfunction when triggered by a button click event?

My attempts to send variables to a PHP file via AJAX when a button is clicked have been unsuccessful. Upon checking my PHP page, I noticed that the variables were not being received. $(document).ready(function(){ $("#qryBtn").click(function(){ ...

The foundation grid system is experiencing difficulties when implemented on an Angular form

After successfully installing Foundation 6 on my Angular project, I am facing an issue with the grid system not working properly. Despite numerous attempts to troubleshoot and debug, I have not been able to resolve this issue. If anyone has any insights or ...

Removing commas and non-numeric symbols from a string using JavaScript

Stripping both a comma and any non-numeric characters from a string can be a bit tricky, especially with RegExs involved :). Is there anyone who could provide assistance on how to achieve this? I need to remove commas, dashes, and anything that is not a ...

Prevent unwanted bouncing and zooming on IOS10+ when using routing in VueJS

Currently, I am developing a Vue.js application that integrates with Three.js to display 3D models. I am using Vue.js with Vuetify as the framework and incorporating the Vue.js router. Due to the nature of displaying 3D models, I need to prevent zooming i ...

Utilizing helper functions in Node based on their specific types

In my helper module, I have different files like content, user, etc. These files define various helpers that can be used in the router. Below is the code for the router: router.js var helper = require("./helper"); function index(response) { response ...

Creating a Directive in Vue.js to Limit Input Fields to Numeric Values

After recently diving into Vue.js, I encountered a challenge. I needed an input field that only accepted numeric numbers; any other value entered by the user should be replaced with an empty string. To achieve this functionality, I decided to create a cust ...

Tips for implementing Javascript form validation

While working on a Django form, I encountered an issue with implementing javascript logic. My goal is to submit the form if the input field is not empty. However, I am struggling to determine how to identify the id of {{form.value}}. <form id = " ...

Why doesn't jQuery UI's addClass method animate visibility like it should?

One of the advantageous features of jQuery UI is its enhancement of the jQuery addClass method, which enables animation by including a second parameter for 'duration', as shown below: $('div').addClass('someclass', 1000); Th ...