Property and Object Javascript

Asked

Viewed 41 times

0

Because he can’t 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>

Error appears:

Uncaught Typeerror: Cannot read Property 'variosDias' of Undefined

  • What do you want to read exactly? the selected value or the DOM?

  • the DOM, I wanted to know where the Error is

  • What error is appearing to you? Here is working normally...

  • Uncaught Typeerror: Cannot read Property 'variosDias' of Undefined

  • I’ll create an answer with an example for you to see how it works.

3 answers

1

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

This Codepen can help you understand better!

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>

Check that your complete code does not have elements with the same name, or that the Names are correctly written.

0


Solved! I put the Javascript code right after the form, and no more error appeared. Thank you all!

<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>

Browser other questions tagged

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