Add javascript to webforms page

Asked

Viewed 108 times

0

I have a method page to insert a javascript in a page.

public void ControleEtiquetaDermaClub(bool visualiza)
        {

            if (visualiza == true)
            {
                String jscript = "";
                jscript = "  $(document).ready(function() { ";
                jscript += "  document.getElementById('mostra-detalhe-derma').style.display = 'block'; ";
                jscript += "   }); ";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", jscript, true);
            }
            else
            {
                String jscript = "";
                jscript = "  $(document).ready(function() { ";
                jscript += "  document.getElementById('mostra-detalhe-derma').style.display = 'none'; ";
                jscript += "   }); ";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", jscript, true);
            }

        }

After the page loaded I have the code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
//<![CDATA[
  $(document).ready(function() {   
   document.getElementById('mostra-detalhe-derma').style.display = 'block';    
   }); //]]>
</script>

Is inserting on the Master page, on the content page I have:

<div id="mostra-detalhe-derma" style="display:none">
   <!--adicionar desconto derma club 24-05-2017 página Tabela.ascx-->
  <a href="/loginloreal.aspx">
         <img id="derma-detalhe" class="img-responsive" alt="derma club" src="../../img/DetalheProduto/dermaclub-vitrine.png"/>
   </a>
</div> 

You’re not changing the style of the div

1 answer

0

The script is running before the creation of the div. The creation of the div must be before the inclusion of the script.

Browser other questions tagged

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