JavaScript Property and Object

0

Why can not he read the property?

objetoSelecionado = document.selectForm.variosDias;
<form name="selectForm">
    <select name="variosDias" multiple>
        <option value="Domingo">Domingo</option>
        <option value="Segunda">Segunda-feira</option>
        <option value="Terça">Terça-feira</option>
        <option value="Quarta">Quarta-feira</option>
        <option value="Quinta">Quinta-feira</option>
        <option value="Sexta">Sexta-feira</option>
        <option value="Sábado">Sábado</option>
    </select>
    <input type="button" value="Enviar" onclick="">
</form>

The error appears:

  

Uncaught TypeError: Can not read property 'severalDays' of undefined

    
asked by anonymous 02.05.2018 / 20:47

3 answers

0

Solved! I put the JavaScript code, just after the form, and no more error appeared. Thanks for everyone!

<form name="selectForm">
    <select name="variosDias" multiple>
        <option value="Domingo">Domingo</option>
        <option value="Segunda">Segunda-feira</option>
        <option value="Terça">Terça-feira</option>
        <option value="Quarta">Quarta-feira</option>
        <option value="Quinta">Quinta-feira</option>
        <option value="Sexta">Sexta-feira</option>
        <option value="Sábado">Sábado</option>
     </select>
</form>
<script>
    objetoSelecionado = document.selectForm.variosDias;
    console.log(objetoSelecionado);
</script>
    
02.05.2018 / 21:14
1

You're on the right track! When you select this way: document.selectForm.variosDia you are only selecting the HTML element, if you want to access the value of it, you must put document.selectForm.variosDia.value . Remember that something must be selected to display, otherwise it will return an empty value;)

This Codepen can help you to understand better!

    
02.05.2018 / 20:58
0

The example is working:

objetoSelecionado = document.selectForm.variosDias;

console.log(objetoSelecionado)

objetoSelecionado.remove()
<form name="selectForm">
    <select name="variosDias" multiple>
        <option value="Domingo">Domingo</option>
        <option value="Segunda">Segunda-feira</option>
        <option value="Terça">Terça-feira</option>
        <option value="Quarta">Quarta-feira</option>
        <option value="Quinta">Quinta-feira</option>
        <option value="Sexta">Sexta-feira</option>
        <option value="Sábado">Sábado</option>
    </select>
    <input type="button" value="Enviar" onclick="">
</form>

Make sure your complete code does not have elements with the same name, or if the names are spelled correctly.

    
02.05.2018 / 20:59