Function Javascript does not work

Asked

Viewed 419 times

0

I have a function that works in my test done in the notepad, but the project does not work.

<head>
    <script type="text/javascript">

        function MostrarEsconderDiv(div) {

            if (document.getElementById(div).style.display == "none") { <---AQUI DA ERRO --->
                document.getElementById(div).style.display = "block";
                document.getElementById(div).value = "aberta";
            }
            else {
                document.getElementById(div).style.display = "none";
                document.getElementById(div).value = "";
            }
        }
    </script>

</head>

<body>
<table>
                               <tr>
                        <td colspan="3" height="10">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3" valign="top">
                            <div class="Section">
                                <div class="SectionHeader">
                                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                        <tr>
                                            <td>
                                                <img align="absMiddle" hspace="4" src="/images/internas/seta3.gif" />
                                                <a id="A3" class="linkFormularios" href="javascript:MostrarEsconderDiv('divSecao6')">Para Sistema Integrado de Gestão, clique aqui</a>
                                                <br />
                                                (abrirá um formulário nesta mesma página para você preencher)
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div id="spacer" style="margin-top: 5px">
                                </div>
                                <div id="divSecao6" style="display:none;  margin-left: 30px" runat="server">
                                    <table id="idSecao6Table" width="100%">
                                        <!-- SECAO 6 -->
                                        <tr>
                                            <td height="15">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="center" bgcolor="#ececec" height="21">
                                                <b>6 - Apenas para Sistema Integrado de Gestão</b>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td bgcolor="#cccccc" height="1">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td height="5">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="center" bgcolor="#f5f5f5">
                                                Responda a esta seção apenas se você estiver interessado em uma certificação integrada (opcional).
                                            </td>
                                        </tr>
                                        <tr>
                                            <td height="5">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <font color="#666666"><b>A Política e o Manual do Sistema de Gestão são integrados?</b></font>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:RadioButtonList ID="rblPoliticaManualSistemaIntegrados" runat="server" RepeatDirection="Horizontal">
                                                    <asp:ListItem Value="Sim" Text="Sim"></asp:ListItem>
                                                    <asp:ListItem Value="Não" Text="Não"></asp:ListItem>
                                                </asp:RadioButtonList>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td height="5">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td bgcolor="#cccccc" height="1">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td height="5">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <font color="#666666"><b>Os procedimentos de gestão (ex: Controle Documentos, Auditorias) são integrados?</b></font>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:RadioButtonList ID="rblProcGestaoIntegrados" runat="server" RepeatDirection="Horizontal">
                                                    <asp:ListItem Value="Sim" Text="Sim, pelo menos 80% dos procedimentos de gestão são integrados"></asp:ListItem>
                                                    <asp:ListItem Value="Não" Text="Não"></asp:ListItem>
                                                </asp:RadioButtonList>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td height="5">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td bgcolor="#cccccc" height="1">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td height="5">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <font color="#666666"><b>Os procedimentos operacionais (ex: Controle de produção) são integrados?</b></font>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:RadioButtonList ID="rblProcOperacionaisIntegrados" runat="server" RepeatDirection="Horizontal">
                                                    <asp:ListItem Value="Sim" Text="Sim, pelo menos 80% dos procedimentos operacionais são integrados"></asp:ListItem>
                                                    <asp:ListItem Value="Não" Text="Não"></asp:ListItem>
                                                </asp:RadioButtonList>
                                            </td>
                                        </tr>
                                        <!-- FIM - ESCONDE SEÇÃO 6  -->
                                    </table>
                                </div>
                            </div>
                        </td>
                    </tr>

inserir a descrição da imagem aqui

  • Have you seen on the console what is the error message it gives?

  • @Joelrodrigues of "Object Required"

  • It works well for me... ---> http://jsfiddle.net/6nj8djdo/ Can you explain what is wrong in this example?

  • @Sergio So outside the application works, but inside does not work.

  • @Andreeh you have this application online so we can see? otherwise it is difficult to reproduce the problem and be able to help.

  • @Sergio, man after a while trying many things, I realized it was Asp.net by runat="server" and then I put Clientidmode="Static" and it worked.

Show 1 more comment

4 answers

1

As I said in the other answer, your error happens, because in your element there is no display attribute defined.

getElementById(div).style.display == "none"

What this line of code means:

"Compare the element display attribute with such ID and compare with None"

Since the display attribute does not exist in the definition of its element, the error is generated.

Correction

Edit the HTML element called by the function and add style="display:none"

For example:

If his element was:

<div id="seuDiv"></div>

he must stay:

<div id="seuDiv" style="display:none"></div>
  • Got it. I added a Clientidmode="Static" in the div that I wanted to get the id and went. Thanks to all.

1


I noticed that you are using ASP.NET Web Forms, right? The ID of some elements is changed at runtime, this may be causing the problem. Put, for example, a alert(div) inside the function to see if the ID is being passed as expected and Inspecione the element in the browser to see if it has the same ID.

  • Puts @Joel Rodrigues, to ligado nisso, mexo com Asp.net, but in this case I’m playing with classic Asp that I don’t know much, and I know a little javascript too but this error got me out of serious rs.

0

Try to change:

<a id="A3" class="linkFormularios" href="javascript:MostrarEsconderDiv('divSecao6')">Para Sistema Integrado de Gestão, clique aqui</a>

For

<span id="A3" class="linkFormularios" onclick="MostrarEsconderDiv('divSecao6')">Para Sistema Integrado de Gestão, clique aqui</span>

Some applications usually bug when we use the link tag...

Edited: The div is browsing on the server and the script running on the client side, track comments

  • No use @jefissu .. : S

  • I think it’s because your div is loaded on the server and you’re trying to hide it from the client then. try to put a runat="server" in the div that runs the function. try also to leave the js function on the server side

  • So, I think that might be it. How do I change that, you know ?

  • Try deleting this server Runar from this div div and mode it to the table, I think q should solve

  • In Asp classico vc need not set any control with runat server, at least I do not use anything running on the server related to controls...

  • So @jefissu, it’s all mixed up this Zica here. I realized this too now and I did the test, I played a Clientidmode="Static" and I got.

  • Okay, just don’t forget to mark the question as resolved then

  • @Andreeh This is from ASP.Net, sign that you are not using classic Asp...

  • @bfavaretto is guy, I realized when I saw the runat="server", then I put the Clientidmode="Static" and it worked.

Show 4 more comments

0

I was able to put Clientidmode="Static" in the element where I needed to take the ID and pass as parameter to Function.

Browser other questions tagged

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