How to activate an onChange using options from a select via Javascript

Asked

Viewed 578 times

0

I have the following select with onChange generating:

<select id="tipo" name="tipo" onchange="gerarImagem(this.options[this.selectedIndex])">
        <option value=""></option>
        <c:forEach items="${FORM.listaTipos}" var="t">
                <option value="${t.codTipo}" id="${t.qtdMaxReg}">${t.descTipo}</option>                 
        </c:forEach>
</select>

And the following init for the page,.

function init() {
     if(document.getElementById('tipo').value != null){
            gerarImagem(document.getElementById('tipo').options[document.getElementById('tipo').value]); //ESTOU PASSANDO O PARÂMETRO OPCOES ERRADO AQUI, O QUE PODE SER FEITO?
    }
}

I have this function generate Image, but I’m not able to manipulate it via javascript since it uses an options parameter. The function works correctly if the option is activated manually, but calling in init does not.

function gerarImagem(opcoes) {
        var spanImagem = document.getElementById('spanImagem');
        var maxReg = opcoes.id; //ERRO
        dwr.util.byId("tdUpload").style.visibility = 'visible';
        dwr.util.byId("tdUploadTxt").style.visibility = 'hidden';


    switch(opcoes.value) ...
}

1 answer

-2

gerarImagem(document   
   .getElementById('tipo')
   .options[document.getElementById('tipo')
   .selectedIndex]
);
  • ?? This line is not wrong, I get error in it because I could not pass the options so can not find options.id

  • 1

    in the options[Document.getElementById('type'). value] snippet tries to exchange value for selectedIndex

  • worked, thanks, edits the main reply I accept the reply

Browser other questions tagged

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