0

I have a variable locally defined in my View. I am setting it to 1 under some conditions. I want to pass this variable to the script in order to have a condition based on it that decides if an audio is played or not.

How can I pass alarm variable to my javascript?

@{
    int alarm = 0;
 }

@foreach (var item in Model)
{
   if (ViewBag.Dtype == "1")
   {                
      alarm = 1;
   }
}

<script type="text/javascript">
    $(document).ready(function () {
    document.getElementById('player').play()});    
</script>

1 Answer 1

1

You can store the Razor variable into javascript variable. Like below, storing alarm into javascript variable jsAlarm. Now you can use jsAlarm for your condition check.

@{
    int alarm = 0;
 }

@foreach (var item in Model)
{
   if (ViewBag.Dtype == "1")
   {                
      alarm = 1;
   }
}
<script type="text/javascript">
    var jsAlarm=@alarm;
    $(document).ready(function () {
    document.getElementById('player').play()});    
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.