0
I have a function to hide a div and open the data with a href. It works in another form normally, but does not work in this form I need it to work, it returns me the following error:
message : "div is not defined"
Follows 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 believe that the problems are in the references, because in another form that does not need these references 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>
<script type="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>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.0/jquery.mask.min.js"></script>
<link rel="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 every time I change, the same thing happens.
What can it be?
problem seems to be that you set the variable
div
in the page load when it is possible that there is no div yet. How to use global variables in this way is not recommended. Try moving getelement into the function or create a function that returns that div and use it– jean
This function of yours is declared where? Your problem is probably that you are performing this function before the
minha_div
exist– Paulo Roberto Rosa
@Jean I managed to solve this way, I changed and it worked, thank you.
– Mariana