Manipulating options from a select using Javascript

Asked

Viewed 999 times

2

I’m making a menu that has an interactive selection list manipulated by Javascript. The problem is that when I change the bread option according to the chosen sandwich nothing happens. Follow the code below

HTML

<select class="ped" id="ped" onchange="pao()">
   <optgroup label="Tradicionais"  id="pedido">
      <option value="301">301 - Super Frango</option>
      <option value="302">302 - Hamburguer</option>
      <option value="303">303 - Precheseburguer</option>
      <option value="304">304 - Eggs Burguer</option>
      <option value="305">305 - Americano</option>
      <option value="306">306 - Bauru</option>
      <option value="307">307 - Misto</option>
      <option value="308">308 - Queijo quente</option>
      <option value="309">309 - Cachorro quente</option>
   </optgroup>
   <optgroup label="Linha especial">
      <option value="310">310 - Sem noção</option>
      <option value="311">311 - Bedegueba</option>
      <option value="312">312 - Fortaleza</option>
      <option value="313">313 - Ceará</option>
      <option value="327">327 - TDB (Tudo de Bom)</option>
   </optgroup>
</select>

Javascript

function pao(){
var sand = document.getElementById('ped');
var opcpao = document.createElement('select');
var p1 = document.createElement('option');
var p2 = document.createElement('option');
var p3 = document.createElement('option');
if (sand.value == 301) {
    //p1.value = 301;
    p1.text = "Árabe";
    //p2.value = 301;
    p2.text = "Bolão";
    sand.appendChild(p1);
    opcpao.appendChild(sand);
} else {

}
    document.body.appendChild(opcpao);
}
  • If possible post your HTML

1 answer

1


You changed the created elements. The correct would be:

opcpao.appendChild(p1); // insere o option no select criado
sand.appendChild(opcpao); // insere o select na div #ped

You also have to call the function when opening the page:

pao();

Getting everything like this:

function pao(){
    var sand = document.getElementById('ped');
    var opcpao = document.createElement('select');
    var p1 = document.createElement('option');
    var p2 = document.createElement('option');
    var p3 = document.createElement('option');

    if (sand.value == 301) {
        //p1.value = 301;
        p1.textContent = "Árabe";
        //p2.value = 301;
        p2.textContent = "Bolão";
        opcpao.appendChild(p1);
        sand.appendChild(opcpao);
    } else {

    }
    document.body.appendChild(opcpao);
}

pao();

Testing:

function pao(){
    var sand = document.getElementById('ped');
    var opcpao = document.createElement('select');
    var p1 = document.createElement('option');
    var p2 = document.createElement('option');
    var p3 = document.createElement('option');

    if (sand.value == 301) {
        //p1.value = 301;
        p1.textContent = "Árabe";
        //p2.value = 301;
        p2.textContent = "Bolão";
        opcpao.appendChild(p1);
        sand.appendChild(opcpao);
    } else {

    }
    document.body.appendChild(opcpao);
}

pao();
<select class="ped" id="ped" onchange="pao()">
        <optgroup label="Tradicionais"  id="pedido">
            <option value="301">301 - Super Frango</option>
            <option value="302">302 - Hamburguer</option>
            <option value="303">303 - Precheseburguer</option>
            <option value="304">304 - Eggs Burguer</option>
            <option value="305">305 - Americano</option>
            <option value="306">306 - Bauru</option>
            <option value="307">307 - Misto</option>
            <option value="308">308 - Queijo quente</option>
            <option value="309">309 - Cachorro quente</option>
        </optgroup>
        <optgroup label="Linha especial">
            <option value="310">310 - Sem noção</option>
            <option value="311">311 - Bedegueba</option>
            <option value="312">312 - Fortaleza</option>
            <option value="313">313 - Ceará</option>
            <option value="327">327 - TDB (Tudo de Bom)</option>
        </optgroup>
    </select> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.