locked
Get Control's id from which jquery is called RRS feed

  • Question

  • I have several radiobuttons.
    I have attached my jquery to this radio buttons
    When i call this jquery i want the control's id from which this jquery is called.

    i tried following technique
    $(this).attr('id');
    but i failed.

    How to find this.
    Thank you
    markand
    Thursday, September 8, 2011 12:05 PM

Answers

  • There no need to do that, as this is the plain DOM object itself.

    Using this.id will be enough. Or if you really want it that way, use $("#" + this.id).attr("id") .


    EDIT: Btw, I went to W3Schools to do some testing, and found that your's should work.

    <html>
    <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("p").click(function(){
        alert($(this).attr("id"));
        $(this).hide();
      });
    });
    </script>
    </head>
    <body>
    <p id="a">If you click on me, I will disappear.</p>
    </body>
    </html>
    

    There should be other problem that make it broken. Will you post the code here?

    Friday, September 9, 2011 1:44 AM

All replies

  • There no need to do that, as this is the plain DOM object itself.

    Using this.id will be enough. Or if you really want it that way, use $("#" + this.id).attr("id") .


    EDIT: Btw, I went to W3Schools to do some testing, and found that your's should work.

    <html>
    <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("p").click(function(){
        alert($(this).attr("id"));
        $(this).hide();
      });
    });
    </script>
    </head>
    <body>
    <p id="a">If you click on me, I will disappear.</p>
    </body>
    </html>
    

    There should be other problem that make it broken. Will you post the code here?

    Friday, September 9, 2011 1:44 AM
  • console.log($("#" + this.id).attr("id"));

    by using this line, we will get control id.

    Thursday, May 10, 2012 7:13 AM