Function being executed from the second click

2

I have a button in my project, which the function is to make appear a div that is like display none, however the function is only executed from the second click on the button, the first click just selects the button. Would anyone know to tell me what I'm doing wrong?

Follow prints:

Screen:

Button:

Firstclick:(Selectedbutton)

Secondclick:(Divappears)

Code:

HTML:

<buttonclass="btn botaoGlobalLeft" ng-click="$ctrl.intermediacao()">
    <span class="glyphicon glyphicon-menu-down"></span> Intermediação</button>

<div class="elemento form-group row" id="intermediacao">
    <div class="row">
        <div class="col-sm-2">
            <label for="apelido2">Apelido</label>
            <input type="text" title="Informe o apelido" id="apelido2" class="form-control inputEstoque">
        </div>
    </div>

CSS:

#informacoes{
display: none;
}

JS (Angular):

intermediacao() {
  const display = document.getElementById("intermediacao").style.display;
  if (display == "none") {
    document.getElementById("intermediacao").style.display = 'block';
  } else {
    document.getElementById("intermediacao").style.display = 'none';
  }
}
    
asked by anonymous 15.02.2018 / 19:39

1 answer

2

An element with display: none direct in CSS, does not have value inline , which is what gets with document.getElementById("intermediacao").style.display .

So in the first click the element gets the style none inline and the second click will work.

To resolve this, enter in if a second condition to check if it is empty:

if (display == "none" || display == "") {...
    
15.02.2018 / 20:20