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>
Mihai Labo
  • 1,082
  • 2
  • 16
  • 40
Pindakaas
  • 4,389
  • 16
  • 48
  • 83
  • 1
    You could at least indent your code and question! – gdoron Jun 21 '12 at 07:14
  • @gdoron some people have lives u know :) – Pindakaas Jun 21 '12 at 07: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) – ThiefMaster Jun 21 '12 at 08: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](http://stackoverflow.com/questions/11124060/how-to-toggle-in-between-2-classes-in-jquery/11124083#comment14576393_11124083). [Another example](http://stackoverflow.com/questions/11048015/jquery-select-all-elements-that-have-an-accesskey-defined/11048059#comment14452181_11048059). I saw so many SO users complaining about this guy. there are other issues with him, but I'll leave them for now. – gdoron Jun 21 '12 at 08:35

1 Answers1

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>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928