How to use javascript code inside if Razor?

1

In my view, I have following code:

<script type="text/javascript">

    @if(Model.File == true)
    {
       //Como usar javascript aqui ?
       //Exemplo:
       $("#target").click(function() {
        alert("Handler for .click() called.");
       });

    }

</script>
    
asked by anonymous 13.06.2017 / 02:06

1 answer

2

Use the <text> pseudo-element to accomplish what you want. Your code would look like this:

<script type="text/javascript">

    @if(Model.File == true)
    {
       //Como usar javascript aqui ?
       //Exemplo:
      <text>
           $("#target").click(function() {
             alert("Handler for .click() called.");
           });
      </text>
    }

</script>

Another way would be to use if outside the script, like this:

@if(Model.File == true)
{
    <script type="text/javascript">
        //Como usar javascript aqui ?
        //Exemplo:
        $("#target").click(function() {
            alert("Handler for .click() called.");
        });
    </script>
}
    
13.06.2017 / 02:23