0

How can i create a id as a newguid for a button and use it in my jquery script? Something like:

   @{ var id = Guid.NewGuid().ToString(); } //

<input type=button id="@id" name="Go" /> @*how can i use this newguid as an id for my button?*@

<script>
            @*how can i use this id to attach an eventhandler to for my button?*@
            $("@id ).click(function(){ 
            });
</script>
4
  • 1
    You could at least indent your code and question! Commented Jun 21, 2012 at 7:14
  • @gdoron some people have lives u know :) Commented Jun 21, 2012 at 7:42
  • 1
    @HanssieTeeuwen: This has nothing to do with having a life or not. Code should be readable and properly indented at all times. A good editor/IDE even helps you with that. It also makes your work easier (so you have more time to "have a life") as you'll spend less time dealing with errors that are quickly spotted in properly indented code (such as missing curly braces). Besides that, you want help and posting properly formatted code will make people more likely to answer (as they don't have to paste it into a code beautifier etc. to read it) Commented Jun 21, 2012 at 8:14
  • @ThiefMaster. THE js-jquery MODERATOR in action. I knew my choice was a good one! BTW, is there a way to track and handle things like that. Another example. I saw so many SO users complaining about this guy. there are other issues with him, but I'll leave them for now. Commented Jun 21, 2012 at 8:35

1 Answer 1

3
@{ var id = Guid.NewGuid().ToString(); }
<input type="button" id="@id" name="Go" />

and the script:

<script type="text/javascript">
    $('#@(id)').click(function() { 
        alert('Thank you for clicking on button with id = ' + this.id);
    });
</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.