Sunday, March 26, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

For loop inside a select – JavaScript – SitePoint Forums

learningcode_x1mckf by learningcode_x1mckf
November 25, 2022
in JavaScript
0
For loop inside a select – JavaScript – SitePoint Forums
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

Hackers Inject Weaponized JavaScript (JS) on 51,000 Websites – GBHackers

4 Ways to Remove a Specific Item From a JavaScript Array – MUO – MakeUseOf

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

Hey.

I have to put a for that I’ve executed inside a choose:

//Consulta a la API para obtener los tipos de recetas
//Forma de atacar al servicio REST
let xmlHttp = new XMLHttpRequest();
xmlHttp.open(
  "GET",
  "http://localhost/recetarioappweb/sistema/API/ingredientes.php",
  false
); // false for synchronous request
xmlHttp.ship(null);
//console.log(xmlHttp.responseText);
 
//Convertimos en objeto
let resultadoIngredientes = JSON.parse(xmlHttp.responseText);
 
let html = "<label>Seleccione uno o varios ingredientes: </label> <br><br>";
 
for (let i = 0; i < resultadoIngredientes.size; i++) 
  html += `<div class="form-check form-check-inline">
    <enter class="form-check-input" sort="checkbox" worth="$resultadoIngredientes[i].id" id='inlineCheckbox1' identify="ingredientes[]">
    <label class="form-check-label" for="inlineCheckbox1">
      $resultadoIngredientes[i].nombre
    </label>
  </div>`;

 
doc.getElementById("ingredientes").innerHTML += html;

Now the elements are with checkbox however I wish to cross it to the choose and contained in the for.

However I have to show the elements in a choose as an alternative of a checkbox and a free textual content enter for the amount subsequent to it. As well as, I additionally have to put an add button so as to add extra elements.

If I press the button it can present me one other choose with one other enter subsequent to it for the amount.

The concept is to output one thing like this:

I’ve tried a thousand methods nevertheless it provides me an error, can somebody assist me?

Thank!!

Earlier than contemplating the JS, I’d begin along with your HTML and work out what it’s essential populate.

For instance

<part id='create-recipe'>
  <type id='add-ingredients'>
    <h1>Create Recipe</h1>
    <label for="recipe-name">Recipe Title</label>
    <enter sort="textual content" identify="recipe-name" id='recipe-name'>
    <h2>Add Components</h2>
    <ul class="added-ingredients">
      <li class="ingredient">
        <fieldset>
          <div class="choice">
            <label for="ingredient-01" class="sr-only">First Ingredient</label>
            <choose identify="ingredient-01" id='ingredient-01'>
              <choice worth="paella-rice">Paella Rice</choice>
              <choice worth="tomatoes">Tomatoes</choice>
              <choice worth="onion">Onion</choice>
              <choice worth="chicken-stock">Rooster Inventory</choice>
            </choose>
          </div>
          <label for="quantity-01" class="sr-only">Quantity</label>
          <enter sort="textual content" class="amount" identify="quantity-01" id='quantity-01' placeholder="quantity">
        </fieldset>
      </li>
      <li class="ingredient">
        <fieldset>
          <div class="choice">
            <label for="ingredient-02" class="sr-only">Second Ingredient</label>
            <choose identify="ingredient-02" id='ingredient-02'>
              <choice worth="paella-rice">Paella Rice</choice>
              <choice worth="tomatoes">Tomatoes</choice>
              <choice worth="onion">Onion</choice>
              <choice worth="chicken-stock">Rooster Inventory</choice>
            </choose>
          </div>
          <label for="quantity-02" class="sr-only">Quantity</label>
          <enter sort="textual content" class="amount" identify="quantity-02" id='quantity-02' placeholder="quantity">
        </fieldset>
      </li>
    </ul>
    <fieldset class="btn-group">
      <button id='add-ingredient'>Add Ingredient</button>
      <button id='send-ingredients' sort="submit">Ship</button>
    </fieldset>
  </type>
</part>

Taking a look at your pattern knowledge, there seems to an ingredient id and an quantity, no identify. The identify presumably comes from one other desk.

While you fetch the info, are you fetching by recipe?
e.g.
`http://localhost/recetarioappweb/sistema/API/ingredientes.php/$recipeName`

Should you change an choice say from hen to onion, are you anticipating the quantity to replace as effectively. Is the quantity an editable enter field?

Concerning the JS I’d think about using the fetch api and async/await

// commonplace async perform for fetching json
const fetchJSON = async (url) => 
  attempt 
    const response = await fetch(url)
    
    if (!response.okay) 
      throw new Error(`HTTP error: $response.standing`);
    
    
    return await response.json()
   catch (err) 
    console.error(err)
  


const getRecipe = async (recipeName) => 
  attempt 
    // are you fetching a particular recipe?
    const recipe = await fetchJSON(
      `http://localhost/recetarioappweb/sistema/API/ingredientes.php/$recipeName`
    )
    
    return recipe
   catch(err) 
    console.error(err)
  


const populateForm = async (recipeName) => 
  // destructure response
  const [name /* string */, ingredients /* array */] = await getRecipe(recipeName)
  
  ...
  
}

It’s also possible to wrap your templates in features. One thing like this.

// returns a string choice
const createOption = (ingredient) => (`
  <choice worth="$ingredient.identify">$ingredient.identify</choice>
`)

// elements.map(createOption) loops by means of the array
// passing every ingredient to createOption and builds an array
// of the returned values
const createOptions = (elements, id) => (`
    <li class="ingredient">
        <fieldset>
          <div class="choice">
            <label for="$id" class="sr-only">Second Ingredient</label>
            <choose identify="$id" id='$id'>
              $elements.map(createOption).be part of('n')
            </choose>
          </div>
          <label for="quantity-$id" class="sr-only">Quantity</label>
        </fieldset>
    </li>
`)



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Hackers Inject Weaponized JavaScript (JS) on 51,000 Websites – GBHackers

by learningcode_x1mckf
March 25, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Hackers Inject Weaponized JavaScript (JS) on 51,000 Websites  GBHackers Source link

Read more

4 Ways to Remove a Specific Item From a JavaScript Array – MUO – MakeUseOf

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

4 Ways to Remove a Specific Item From a JavaScript Array  MUO - MakeUseOf Source link

Read more

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Toolkit Allows JavaScript Devs to Program Embedded Devices  The New Stack Source link

Read more

Select data value from grandparent div? – JavaScript – SitePoint

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Select data value from grandparent div? - JavaScript  SitePoint Source link

Read more

How to Handle Errors in JavaScript – Programming – MUO – MakeUseOf

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Handle Errors in JavaScript - Programming  MUO - MakeUseOf Source link

Read more
Next Post
VMware Brings Java 17 Support to Spring Boot Framework

VMware Brings Java 17 Support to Spring Boot Framework

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Jamstack Panel: Multiple JavaScript Frameworks Are a Good Thing – The New Stack

February 24, 2023
Proposed Java API would enable sharing of immutable data across threads

Proposed Java API would enable sharing of immutable data across threads

October 2, 2022

High Satisfaction Among JavaScript Developers

January 13, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • 2023 Java roadmap for developers – TheServerSide.com
  • YS Jagan launches Ragi Java in Jagananna Gorumudda, says focused on intellectual development of students – The Hans India
  • Disadvantages of Java – TheServerSide.com

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?