2
         *@ JAVASCRIPT
             $(document).ready(function()
             {
             }
             )

             function videochange(url) {
                 alert(url)
             }

On the click of this button it wont hit the JavaScript function above. Any idea why? I really need to get that video.Value to a JavaScript function.....

 @foreach(var video in Model.VideoList)
        {           
            var url = video.Value;
         <input type="button" value="@video.Text" onclick="javascript:videochange(url);" />
            <br />
        }
0

3 Answers 3

2

I would use JQuery. Assuming you've got version 1.7 or later, the following code should work. It also enforces a better separation between your markup and your JavaScript.

<div class="container">
  @foreach(var video in Model.VideoList) {           
    <input type="button" value="@video.Text" data-url="@video.Value" />
  }
</div>

<script>
  $(document).ready(function(){
    $('.container').on('click', 'input[type="button"]', function(){
      var url = $(this).attr('data-url');
      alert(url);
    });
  });
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

I'd probably add [data-url] to the button selector, so only buttons with a data-url get that click handler.
@cHao Yeah, that's even better.
1

Try this

@foreach(var video in Model.VideoList)
{           
    var url = video.Value;
 <input type="button" value="@v.Value" onclick="@String.Format("videochange('{0}')", url)" />
    <br />
}

1 Comment

the funny thing is this worked at one point now its giving me the undetermined string constant again... and the second ' seems to not realize theres a first ' it makes the ); after count as the string.
1

Try

@foreach(var video in Model.VideoList) {           
    <input type="button" value="@video.Text" onclick="videochange('@video.Value');" />
    <br />
}

3 Comments

@CoreyToolis sorry updated, there was another error. onclick="javascript:videochange should be onclick="videochange
@ArunPJohny: The javascript: thing is ugly, but not a syntax error. It gets parsed as a label.
the funny thing is this worked at one point now its giving me the undetermined string constant again... and the second ' seems to not realize theres a first ' it makes the ); after count as the string.

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.