message: "div is not defined"

0

I have a function to hide a div and open the data with a href. It works in another form normally, but it does not work in this form that I need that works, it returns me the following error:

  

message: "div is not defined"

Follow the function:

window.onload = function() {
  // Localiza o elemento
  var div = document.getElementById('minha_div');
  // Esconde a DIV
  div.style.display = 'none';
  // O link
  var clique = document.getElementById('clique');

  // Captura o evento de clique no link
  clique.onclick = function() {
    alert('clique');
    // Verifica se getComputedStyle é suportado
    if ('getComputedStyle' in window) {
      var display = window.getComputedStyle(div).display;
    } else {
      // Obtém a opção display para navegadores antigos
      var display = div.currentStyle.display;
    }

    // Verifica se display é none
    if (display == 'none') {
      // Muda para display block
      div.style.display = 'block';
    } else {
      // Muda para display none
      div.style.display = 'none';
    }

    // Retorna falso para não atualizar a página
    return false;
  }
}

html:

<span class="fundo_clique">
    <a href="#" id="clique">Para configurar as mensagens, clique aqui!</a>
</span>
<br />
<%-- MODAL FINANCEIRO LANÇAMENTO DE CONTAS --%>
<span id="minha_div" style="border: none" runat="server">*conteúdo*</span>

I think the problems are in the references, because in another form that does not need these references it works, and in this I need these:

<link rel="stylesheet" href="css/mobile/pessoas_small_1366.css" />
<script type="text/javascript" src="javascript/jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scripttype="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js"></script><scripttype="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.0/jquery.mask.min.js"></script><linkrel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css" />
<script src="javascript/pessoa.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" />

But in every way I change, the same thing happens.

What can it be?

    
asked by anonymous 21.12.2017 / 14:07

4 answers

4

First problem (not related), has two jQuery without need:

<script type="text/javascript" src="javascript/jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Or use one or use another

Second, the error does not seem to be in the code described above, because even if the element does not exist on the page the error that occurred would be something like:

  

Can not read property 'style' of null

Already the error

  

is not defined

Occurs when the variable does not exist in the scope of a function, so you probably have some other variable div in other script , that is $.ready and window.onload solution, the problem is the access variable.

    
21.12.2017 / 15:59
1

Simple, you have the div RUNAT = SERVER , so the ID will change why this div runs server side, try to get the class

 <span class="minha_div" id="minha_div" style="border: none" runat="server">*conteúdo*</span>

And in your .js

$(document).ready(function(){
  var minhaDiv = $('.minha_div'); 
});
    
21.12.2017 / 16:27
0

Once you're including Jquery try this:

$(document).ready(function(){
  var div = $('#minha_div');
  div.css( "display", "none" );
  resto do seu código....   
});

Remove your onload and try $ (document) .ready

The difference is that the div will only be hidden when the DOM has been read. So it is called this function by callback that hides its element. The callback will prevent the function from executing before the html is fully loaded in the browser since javascript is an asynchronous language.

What are Asynchronous Flows?

Although designed to have a single run-time flow, some tasks in your JavaScript program can be executed without interfering with the main flow of code execution. These tasks are known as asynchronous flows. You can trigger a series of these tasks without having to wait for each to complete to proceed. On current computers that have more than one processing core, some tasks within the scope of your program may even run at the same time as your code.

    
21.12.2017 / 14:43
0

If you use multiple libraries (jQuery, Bootstrap and related), it would make more sense for you to use jQuery itself to simply show and hide div , drastically reducing the code, and not even having to check Compatibility with if ('getComputedStyle' in window) .

In addition, with the code below suggested, no error will be returned in relation to div (at least in the example given in the question), whereas functions and variables within $(window).on('load', function(){ will only be invoked after loading of the page.

See that with just 9 lines with jQuery, you can do the same thing you want to do with 20 lines of JavaScript code:

$(window).on('load', function(){
   var div = $('#minha_div'); // Localiza o elemento
   div.hide(); // Esconde a DIV
   $('#clique').click(function(e){ // Captura o evento de clique no link
      alert('clique');
      div.is(':visible') ? div.hide() : div.show(); // pega o estado da div e mostra/esconde
      e.preventDefault();
   });
});

Example:

$(window).on('load', function(){
   var div = $('#minha_div'); // Localiza o elemento
   div.hide(); // Esconde a DIV
   $('#clique').click(function(e){ // Captura o evento de clique no link
      alert('clique');
      div.is(':visible') ? div.hide() : div.show(); // pega o estado da div e mostra/esconde
      e.preventDefault();
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><spanclass="fundo_clique">
   <a href="#" id="clique">Para configurar as mensagens, clique aqui!</a>
</span>
<br />
<span id="minha_div" style="border: none" runat="server">*conteúdo*</span>
    
21.12.2017 / 17:53