if error inside if

Asked

Viewed 619 times

0

I have a problem, in my view the logic is correct, however it is not working, I have a select that has to be checked and if it is selected certain option it opens another select, in that other select depending on the information selected it validates if there is something written in the input field, the error is that it is not checking if there is something in that input and it seems that it jumps that if and already goes to Else.

Follow the code I’m trying to:

var aoption = document.getElementById("options").value;
var aopcDemaisTemAniver = ['Aniversario de Nascimento', 'Aniversario de Namoro', 'Aniversario de Casamento', 'Aniversario de Mêsversario', 'Outras Opções'];
var aoptionAni = document.getElementById("options2").value;
var ademaisOpcSex = ['Aniversario de Nascimento', 'Aniversario de Mêsversario'];
var ainput6 = document.getElementById("IdadePeriodo3").val()== null;

if(aoption == 'Aniversario' && !aopcDemaisTemAniver.includes(aoptionAni)){
    swal({
      title: "Ops.. Algo está errado !",
      text: "Para finalizar a compra é necessario selecionar qual o tipo de Aniversario.",
      button: "Continuar",
    });
    document.formMonteCaixa.options2[0].focus();


       }else if(aoption == 'Aniversario' && !aopcDemaisTemAniver.includes(aoptionAni) && !ademaisOpcSex.includes(aoptionAni) && ainput6.length<1){
      swal({
        title: "Ops.. Algo está errado !",
        text: "Para finalizar a compra é necessario informar o Idade / Periodo.",
        button: "Continuar",
      });
      document.formMonteCaixa.IdadePeriodo3[0].focus();


  }else{
    alert(ainput6);
    document.getElementById('BlocoDeEnvioEmailPag').style.display = 'block';
  }


<select id="options" name="options"  class="cc-selector" onchange="optionCheck()">
        <option value="Selecione o Tema:" disabled selected>Selecione o Tema:</option>
        <option value="Aniversario">Aniversário</option>
        <option value="Pedido Casamento">Pedido Casamento</option>
        <option value="Gravidez">Gravidez</option>
        <option value="Nascimento">Nascimento</option>
        <option value="Dia das Mães / Pais">Dia das Mães / Pais</option>
        <option value="Outros">Outros</option>
      </select>
        <div id="hiddenDiv" class="Aniversario" style="display:none;">
            <select id="options2" name="options2" onchange="optionCheck()">
                <option value="Aniversario" disabled selected>Aniversário de:</option>
                <option value="Aniversario de Nascimento">Aniversário de Nascimento</option>
                <option value="Aniversario de Namoro">Aniversário de Namoro</option>
                <option value="Aniversario de Casamento">Aniversário de Casamento</option>
                <option value="Aniversario de Mêsversario">Aniversário de Mêsversario</option>
                <option value="Outras Opções">Outras Opções</option>
            </select>
        </div>

<div id="hiddenDiv4_1" class="OpçõesAniversarioComSex cc-selector" style="display:none;">
        <div class="ModeloCaixaBloco6Col1 ModeloCaixaBloco6Col1K">
         <input type="text" id="IdadePeriodo3" name="IdadePeriodo3" placeholder="Idade / Periodo"  onblur="optionCheck(this);">
        </div>
</div>

3 answers

4


The error is in the syntax of this line:

var ainput6 = document.getElementById("IdadePeriodo3").val()== null;

The method val() is to take value of jQuery objects. In your case, to take the value of the object, you would have to use value:

var ainput6 = document.getElementById("IdadePeriodo3").value == null;

Also don’t use null to check if there is value in the field. Check this way if it is empty:

var ainput6 = document.getElementById("IdadePeriodo3").value == "";
  • Thank you very much, I entered this and made a small change in my if e agr is working, if anyone needs it this way: (aoption == 'Anniversary' && ademaisOpcSex.includes(aoptionAni) && ainput6 == true)

  • @dvd did not know this icon , that cool :)

  • 1

    Oh yes, about the icons, you can copy from https://getemoji.com/ and paste here rs

  • @dvd vi agora, you forgot to mention me, so I don’t get the notifications.

  • @dvd because in developer mode (F12) I cannot see the code of the icon ? Only drawing appears. How bizarre !!!

  • @Matheusmiranda Well, man, I don’t know much about it... it seems like an image but it’s not, it’s like it’s some character

  • @dvd I think I’ll ask this question here at stackoverflow

  • @Matheusmiranda I thought about doing it a few days ago, but I let it go

Show 3 more comments

2

Take and put values in the input with .value instead of .val()
Try document.getElementById("elemento").value

  • I had already made this change before direct in the code, but to put here my doubt had used the old code of a notepad unfortunately, but thank you very much for commenting :D, I changed my if tbm if anyone needs: (aoption == 'Anniversary' && ademaisOpcSex.includes(aoptionAni) && ainput6 == true)

0

In case anyone needs it That’s the way it was:

var ainput6 = document.getElementById("IdadePeriodo4").value == "";

if(aoption == 'Aniversario' && ademaisOpcSex.includes(aoptionAni) && ainput6 == true){

Browser other questions tagged

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