Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Return elements id

    Im working on a function where i need to return the active elements ID.
    the element will have class="main" id="1" onmouseover="showSub()">

    Would someone be able to write me a simple function which simply puts the elements ID number in an alert box? I can figure out the rest i need from that .

    Thanks, Tom
    Last edited by tomharto; 07-24-2011 at 10:50 PM.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,865
    Thanks
    6
    Thanked 1,029 Times in 1,002 Posts
    Just for the records: An ID must not start with a number, it must begin with a letter A–Z or a–z.

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Ahh okay thanks . In the end i gave up on ID and i did showSub(1) then function showSub(theID) { etc.

  • #4
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    884
    Thanks
    173
    Thanked 101 Times in 101 Posts
    Hi

    a simple function which simply puts the elements ID number in an alert box?
    Code:
    <script type="text/javascript">
    function showSub(id){
    alert("The id is " + id);
    }
    </script>
    <input type="text" name="test1" class="main" id="A122334" onmouseover="showSub(this.id)" />

  • Users who have thanked low tech for this post:

    tomharto (07-25-2011)

  • #5
    New Coder
    Join Date
    Jul 2011
    Location
    Chennai, India
    Posts
    23
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Hi,

    If you are comfortable with JQuery, you can use the below piece of code and get the ID of any element you like.

    <!-- latest jquery include -->
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" ></script>

    <!-- function to return the element Id -->
    <script type="text/javascript">
    $('.hello').live('click', function() {
    alert('ID of the clicked element - '+ $(this).attr('id'));
    });
    </script>

    <!-- In html body add the code -->
    <div id="div1-clicked" class="hello">Div Click</div>
    <label id="label1-clicked" class="hello">Label Click</label>

    The above example for 'click' event, for mouseover change 'click' to 'mouseover' in the script.

    Hope that this will solve your problem.
    I also attached the html file for your reference.
    Attached Files Attached Files

  • Users who have thanked nanda.t for this post:

    tomharto (07-26-2011)

  • #6
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,865
    Thanks
    6
    Thanked 1,029 Times in 1,002 Posts
    I must say, it’s really stupid to install a 31KB library to perform such a basic task.

  • #7
    New Coder
    Join Date
    Jul 2011
    Location
    Chennai, India
    Posts
    23
    Thanks
    0
    Thanked 5 Times in 5 Posts
    I'll use libraries in my works and I feel comfortable with those libraries rather than using plain js. It's upto them whether to use it or not.

    If anyone calls me stupid because of using libraries, I'll be happy to be a stupid.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •