SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: getelementbyid

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getelementbyid

    Hi,

    Is there anywhere on the web that I can get a listing of all the attributes of getelementbyid? I've seen things like getelementbyid('me').innerHtml or get....().style. But how do I get to know all the other attributes and what they do?

    Any help appreciated.

    CathyM

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    getElementById(id) returns an element from the Document Object Model of your html (or xml) page.

    If the element is a <p> it will have certain properties, if an <input type="button"> then others.

    Here's a handy reference from w3schools
    http://www.w3schools.com/TAGS/tag_select.asp

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you know what the valid elements and attributes are in HTML then you know what the valid properties are when you access an element in the HTML via its id.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I presume that you're wanting to learn more about working with the DOM (document object model).

    PPK has some very good information about the DOM
    http://www.quirksmode.org/js/contents.html#dom

    Read through the different sections, and please don't hesitate to ask any questions that can help further your knowledge.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, the links have proved useful in my getting a better understanding of JavaScript.

    I am looking at some code and I am getting an error with the following line,

    if(document.getElementById(myForm).elements[i].type == 'checkbox'){

    Firefox reports it as 'document.getElementById(myForm) has no properties', but it may be that the syntax is correct and the problem lies in seeing the form.

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the script running before the page has finished loading? It could be that when document.getElementById(myForm) runs the form has not yet appeared on the page.

    Or should document.getElementById(myForm) be document.getElementById("myForm")?

    It's hard to tell without seeing your page.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    If the script is running from the head of the document, the rest of the document won't be available to the script. Run the script from the end of the body, just before the </body> tag.

    The other issue that r51 brought up relates to myForm term.

    Code html4strict:
    <form id="myForm">
        ...
    </form>

    There are two common ways to access the form, and here is how they differ.

    The first way is to explicitly specify the identifier of the form

    Code javascript:
    if (document.getElementById('myForm').elements[i].type == 'checkbox') {

    The other way is to store the name of the form in a variable, then access that later on. This technique is great because if the name of the form changes, there's only one place in the code that you need to make the change.

    Code javascript:
    var myForm = 'myForm';
    ...
    if (document.getElementById(myForm).elements[i].type == 'checkbox') {
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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