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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Accessing the ID of an element

    Hello,

    I am aware that it is possible to access form elements by their ID e.g.
    Code:
    document.getelementbyID("IDname");

    However is there a way to return the name of an element and store it in a variable?

    For example I have a list box and on selecting an option, I would like to display the ID of that option. Any help would be appreciated.

    Thanks

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,623
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Options within a select list are distinguished by their <b>value</b> and <b>text</b>. The options are just a part of the select and not separate elements and those are the only two properties that are defined as arrays rather than individual fields.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Mar 2006
    Location
    São Paulo, Brazil
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In this case, document.getElementById won't work, just as felgall said before.

    But there is another way for you to get the ID of an option.

    You can put anything you want in the tags of any element.

    if you write <inptu type="button" id="X" VaRiAbLe="This is a variable" />, alert(document.getElementById("X").VaRiAbLe) will return "This is a variable".

    With options you can't do this, but you can access an option that can have a specific id just making a loop throughout the <select> tag.

    For example:
    Code:
    <select name="selecao">
    	<option>opt 1</option>
    	<option id="opt2">opt 2</option>
    </select>
    
    <script>
        for( var i=0; i<document.getElementById("selecao").options.length; i++)
        {
             if( document.getElementById("selecao").options[i].id == 'opt2' )
                  alert(document.getElementById("selecao").options[i]);
        }
    </script>
    It's not the fastest way, but it will work fine.

    I hope this can be useful for you. =)
    ----
    As coisas mais simples são as mais importantes.
    ---------
    Thompson M1A1: D-Day
    ----------------

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,623
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Of course if you add your own attributes like that then the code will not work in some browsers and it wont pass validation (unless you are using XML and define your own doctype).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Mar 2006
    Location
    São Paulo, Brazil
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hum.... I didn't get it... can you show us an example??
    ----
    As coisas mais simples são as mais importantes.
    ---------
    Thompson M1A1: D-Day
    ----------------


  •  

    Posting Permissions

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