Jump to content
Fórum Script Brasil
  • 0

[Resolvido] Mudar valor de select dentro da tabela especificada


dinohills

Question

Boa tarde,

Estou tentando montar um script que ao escolher uma opção na lista primaria(select) ele mude o valor das demais listas(select) que estão dentro da tabela.

Estou tentando definir isso pegando a id da tabela e fazendo com que ele mude o valor das listas que estão dentro da tabela que foi especificada.

O script me retorna o seguinte erro:

tabela.getElementsByName is not a function

O problema é que eu preciso manter os nomes das listas(select) tanto da tabela 1 como da tabela 2 iguais.

Tem como restringir isso pegando o id da tabela e mandando alterar so dentro dela ?

Segue o exemplo:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title></title>
            &lt;script type="text/javascript">
                function muda_lista(id_tabela,id_lista,nome_lista){
                    var tabela = document.getElementById(id_tabela);
                    var lista_principal  = document.getElementById(id_lista);
                    var listas = tabela.getElementsByName(nome_lista);

                    for(i=0;i<listas.length;i++){
                        listas[i].value = lista_principal.value;
                    }

                }
            </script>
    </head>
    <body>
        <table border="1" id="tabela1">
            <thead>
                <tr>
                    <th>
                        Tabela 1<br/>
                        <select id="lista_principal1" onchange="muda_lista('tabela1','lista_principal1','lista')">
                            <option value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                        </select>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <select name="lista">
                            <option value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <select name="lista">
                            <option value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <select name="lista">
                            <option value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                        </select>
                    </td>
                </tr>
            </tbody>
        </table>
        <br/>
        <table border="1" id="tabela2">
            <thead>
                <tr>
                    <th>
                        Tabela 1<br/>
                        <select id="lista_principal2" onchange="muda_lista('tabela2','lista_principal2','lista')">
                            <option value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                        </select>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <select name="lista">
                            <option value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <select name="lista">
                            <option value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <select name="lista">
                            <option value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                        </select>
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

Edited by dinohills
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...