Is there a way to showcase a dynamically created <select> element in a single row or line?

I am struggling to display a list of ingredients in three columns, as they are not aligning correctly. Can someone offer a solution? It may seem complex, but I am at a loss.

function display(){
var lght = jsn[0].ingredient.length; 
var old_val = new Array(lght);
var ell =  "";
var ul = document.getElementById("comp_tag");
for(let a=0; a < lght; a++){
    var li = document.createElement('li');
    var divr = document.createElement('div');
    li.setAttribute("id","lineid_"+a);
    divr.setAttribute("class","row align-middle");
    ul.appendChild(li);
    li.appendChild(divr);
    var col_one = document.createElement('div');
    col_one.setAttribute("class","col-6 text-left");
    col_one.setAttribute("id","col1"+a);
    divr.appendChild(col_one);
    col_one.appendChild(document.createTextNode("value 1"));
    var col_two = document.createElement('div');
    col_two.setAttribute("class","col-3 text-right");
    col_two.setAttribute("id","col2"+a);
    divr.appendChild(col_two);
    col_two.appendChild(document.createTextNode("value 2"));
    var col_three = document.createElement('div');
    col_three.setAttribute("class","col-3 text-left");
    col_three.setAttribute("id","col3"+a);
    divr.appendChild(col_three);
    var pb = init_workspace(jsn[0].ingredient[a].type);
    var sel = document.createElement("select");
    sel.setAttribute("class","custom-select");
    sel.setAttribute("id","c_select"+a);
    col_three.appendChild(sel);
    for(var t=0; t<pb.length; t++){
        var optn = document.createElement("option");
        if(jsn[0].ingredient[a].unit == pb[t].name){
            optn.setAttribute("selected","")
        };
        optn.setAttribute("value",pb[t].name);
        sel.appendChild(optn);
        optn.appendChild(document.createTextNode(pb[t].name));
    }

My HTML section looks like this:

<div class="col-md text-right" id="divid">
        <p class="text-uppercase"><b>ingredients</b></p>
        <ul id="comp_tag">
        </ul>
</div>

Answer №1

Without the jsn object, I cannot recreate this example exactly as shown. However, I will provide a static representation below...

To achieve a row layout for block-level items, make the parent element of your list items a flex container. In this case, I have used CSS to target a UL and set display: flex;.

#divid #comp_tag {
  display: flex;
}

Below is a simple snippet that demonstrates creating an object and displaying its items in a row by using display:flex on the parent UL element.

I hope this explanation helps...

let obj = {
  0: {
    'name': 'Cajun Delight',
    'ingredients': {
      'type': {
        'salt': '1 teaspoon',
        'garlic': '2 cloves',
        'pepper': '1 pinch'
      }
    }
  },
  1: {
    'name': 'Midwest Bland',
    'ingredients': {
      'type': {
        'salt': '1 teaspoon',
        'garlic': '2 cloves',
        'pepper': '1 pinch'
      }
    }
  },
  2: {
    'name': 'Asian Stir Fry',
    'ingredients': {
      'type': {
        'salt': '1 teaspoon',
        'garlic': '3 cloves',
        'pepper': '1 pinch',
        'soy_sauce': '1/4 cup'
      }
    }
  }
}


function displayInfo() {
  let compTag = document.getElementById("comp_tag");
  for (let values in obj) {
    let li = document.createElement("LI");
    li.textContent = obj[values].name;
    compTag.append(li);
    compTag.style.justifyContent = "space-around";

    if (obj[values].ingredients.type) {
      for (let type in obj[values].ingredients.type) {
        let subUl = document.createElement("UL");
        let subLi = document.createElement("LI");
        li.append(subUl);
        subUl.append(subLi);
        subLi.textContent = `${obj[values].ingredients.type[type]}`;        
      }

    }

  }
}

displayInfo();
#divid #comp_tag {
  display: flex;
  list-style-type: none;
}
<div class="col-md text-right" id="divid">
        <p class="text-uppercase"><b>ingredients</b></p>
        <ul id="comp_tag">
        </ul>
</div>

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

Is there a way to retrieve the initial value from the second element within the JSON data?

Exploring JSON Data Collections In my JSON data, I have two collections: FailedCount and SucceededCount. { "FailedCount": [{ "FailedCount_DATE_CURRENT_CHECK": "2016-11-30 10:40:09.0", "FailedCount_DATE__CURRENT__CHECK": "10:40:09", "FailedCo ...

ReplaySubject in Angular is failing to update the array when a new object is added

I am encountering an issue where, upon attempting to create a new page Object, it successfully sends the data to the backend but does not update the array. I have to refresh the page in order to view the entire array. Within the frontend, I am utilizing O ...

Looking to update table content dynamically using jQuery ajax?

I need assistance in creating a dynamic table that can update its content based on the company selected from a dropdown menu. The data should be fetched using AJAX and only display information related to the chosen company. Furthermore, I would like to imp ...

Tips for properly nesting a "carousel" div within an "oe_structure" without any overlapping issues

Having trouble with placing a "carousel" div inside the "oe_structure" in my odoo template. I am currently working on editing the "website_sale.products" template. As a newcomer to Odoo, can I use inheritance in my direct code to make edits? I'm stil ...

Associate a click event to a dropdown menu element to trigger on selection

My issue involves having multiple select elements. When one of them is changed, I am trying to bind a click event only to its next element with the .btn class. Below is the code snippet illustrating my problem: <div class="whole"> <ul> ...

Exploring directories for files using Node.js

Currently, I am attempting to locate specific files within a designated folder on Windows using node and grunt. I have a grunt task set up with a function to read a directory containing JSON files. However, when I execute the task, the code for reading th ...

Verify changes in the Cross Domain RSS feed by utilizing Ajax technology

I have a website where I am trying to automatically reload an RSS news feed from every 60 seconds if it has been updated. I attempted to use Ajax for this purpose, but I seem to be facing some issues: <script type="text/javascript" src="../js/jquery.a ...

A step-by-step guide on creating a chainable command in Cypress

Imagine having a variable called username. Now, consider a chainable function that needs to verify whether the username is empty or not. Original Method: if(username !== "") { cy.get('#username').type(username) } Expected Outcome: ...

The error "Uncaught TypeError: Cannot read property 'appendChild' of undefined" occurs in HTML DOM and JavaScript

I have developed a function to extract the most popular articles from a website. This involves scraping two parts: one for the article image and another for the link and title of the article. I then combine these elements into separate objects containing ...

Exploring object properties to access JSON arrays

Consider this scenario with a JSON file: { "shows": [ { "name": "House of cards", "rating": 8 }, { "name": "Breaking bad", "rating": 10 } ] ...

Tips for verifying the errors list in an ajax response

Using ajax and php, I am creating a post feature that allows users to upload images along with text. While my php code is currently working fine in checking the content of the post including images and validating them, I have encountered an issue. The pr ...

How can timestamps be set in MongoDB during an insertOne() operation without relying on new Date() as in PostgreSQL's NOW()?

timestamp: { $currentDate: { $type: "timestamp" } } }); The insert() method is not functioning properly Is there anyone who can assist me with this issue? ...

Verifying file types with HTML5 drag and drop feature

Is it possible to change the drop zone's background color to green or red based on whether the dragged payload contains supported file types (JPEG)? Do Gecko and Webkit browsers have the ability to determine the file type of drag and drop files? ...

What guidelines should be followed when using nested ternary operators in code formatting?

Consider a scenario with nested ternary statements: return foo ? 1 : bar ? 2 : 3; How can we effectively format this code for better readability by future viewers? ...

Connect cells within an HTML table by drawing a line between them

While attempting to create a visually appealing floor plan, I have been unable to find any Javascript/jQuery libraries that meet my needs. My input is a text file with 0s and 1s (a sample 10x10 matrix), where the 1s represent walls. After processing the in ...

Tips for preserving login session continuity following page refresh in Vuejs

signInMe() { this.$store.dispatch('setLoggedUser', true); this.$store.dispatch('setGenericAccessToken', response.data.access_token); this.errorMessage = ""; }, (error) => { if (error) { th ...

Tips for saving two text inputs using a JavaScript function into a single .txt file

Currently, I'm developing a webpage where users can input values into two textarea fields. Upon clicking the submit button, the program should trigger a download of both text inputs and store them in a text file. While I have successfully managed to s ...

Applying formatting options to an element inserted using the Write() method

After a user clicks on an image, I want them to see a large blueish button with text on it. Here is the javascript code I used with the Write() method: <script> function DoThis() { s="<button style=background-color:#aaccdd; font-size:50px; >He ...

Sending post forms securely using HTTPS instead of HTTP

Here is the code snippet located on : <form id="MainForm" name="MainForm" method="post" action="xyz.aspx"> We now want to redirect the user to . However, we are unable to use the full URL as we need to implement the same code across all environment ...

What is the best way to create a test for my Vue component using Jest?

Below is the login form component that needs to be tested for various scenarios: Verify successful login with both username and password filled Check for input error when either username or password is left blank Ensure input error is thrown when only us ...