Check the value of an element and take action Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Check the value of an element and take action without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I would like a function that checks if some fields of the form have been filled out and if true it would execute a code of mine, if false it would do nothing.

In short, I would like to know how I check the fields, the code that will be executed, I already have it ready.

HTML of the fields I want to check if they have been filled out:

<!-- verifica select -->

<div class="form-group pmd-textfield col-md-4">
  <label for="InqueritoCircunscricaoPolicial" class="control-label cssPerguntas espacamentoLabels">
    <strong>1.1</strong> Circunscrição policial
  </label>
  <select asp-for="InqueritoModel.COD_INQUERITO_CIRCUNSCRICAO_POLICIAL "
          asp-items="Model.ListCircunscricao"
          id="InqueritoCircunscricaoPolicial"
          class="select-codigo"
          onchange="mostrarOutroDeCombo(this, 'DSC_OUTRO_INQUERITO_CIRCUNSCRICAO_POLICIAL')">
    <option value="">Selecione a circunscrição</option>
  </select>
</div>

<!-- verifica input -->

<div class="form-group pmd-textfield col-sm-3">
  <label for="NumeroDaOcorrencia" class="control-label cssPerguntas espacamentoLabels">
    <strong>1.3</strong> Número da ocorrência
  </label>
  <input class="form-control" id="NumeroDaOcorrencia" maxlength="1000" placeholder="N° da ocorrência"
         asp-for="InqueritoModel.NumeroDaOcorrencia"
         type="text">
</div>

<!-- verifica input -->

<div class="form-group pmd-textfield col-sm-3">
  <label for="DataDaInstauracao" class="control-label cssPerguntas espacamentoLabels">
    <strong>1.9</strong> Data da instauração
  </label>
  <input class="form-control" id="DataDaInstauracao" min="1900-01-01"
         asp-for="InqueritoModel.DataDaInstauracao"
         type="date">  
</div>

Answer

verifyonClick = () => {
var first = document.getElementById("InqueritoCircunscricaoPolicial")
var second = document.getElementById("NumeroDaOcorrencia")
var third = document.getElementById("DataDaInstauracao")
if(first.value && second.value && third.value)
alert("Value exist in every input")
else
alert("Please enter value in all fields")
}
<!-- verifica select -->

<div class="form-group pmd-textfield col-md-4">
  <label for="InqueritoCircunscricaoPolicial" class="control-label cssPerguntas espacamentoLabels">
    <strong>1.1</strong> Circunscrição policial
  </label>
  <select asp-for="InqueritoModel.COD_INQUERITO_CIRCUNSCRICAO_POLICIAL "
          asp-items="Model.ListCircunscricao"
          id="InqueritoCircunscricaoPolicial"
          class="select-codigo"
          onchange="mostrarOutroDeCombo(this, 'DSC_OUTRO_INQUERITO_CIRCUNSCRICAO_POLICIAL')">
    <option value="">Selecione a circunscrição</option>
  </select>
</div>

<!-- verifica input -->

<div class="form-group pmd-textfield col-sm-3">
  <label for="NumeroDaOcorrencia" class="control-label cssPerguntas espacamentoLabels">
    <strong>1.3</strong> Número da ocorrência
  </label>
  <input class="form-control" id="NumeroDaOcorrencia" maxlength="1000" placeholder="N° da ocorrência"
         asp-for="InqueritoModel.NumeroDaOcorrencia"
         type="text">
</div>

<!-- verifica input -->

<div class="form-group pmd-textfield col-sm-3">
  <label for="DataDaInstauracao" class="control-label cssPerguntas espacamentoLabels">
    <strong>1.9</strong> Data da instauração
  </label>
  <input class="form-control" id="DataDaInstauracao" min="1900-01-01"
         asp-for="InqueritoModel.DataDaInstauracao"
         type="date">  
</div>

<div>
<button onclick="verifyonClick()"> submit </button>
</div>
We are here to answer your question about Check the value of an element and take action - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji