SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    document.getelementby function

    I have a function that does several document.getElementById so I want to write a function for it, and I need to access different properties of the element based on the situation, so I created a function but is not working and would like to know if someone can help with it

    I tried this:

    Code JavaScript:
    function getTheValue(id, the_property) {
    	return document.getElementById(id).the_property;
    }

    And this:

    Code JavaScript:
    function getTheValue(id, the_property) {
    	//alert(id + the_property);
    	if (the_property === 'value') {
    		document.write(document.getElementById(id).value);
    	} else if (the_property === 'innerHTML') {
    		document.write(document.getElementById(id).innerHTML);
    	}
    }

    neither works and I donīt know what Iīm doing wrong, before I had a function only for the value of the element but want to be able to access other properties, the function that is currently working for me is:

    Code JavaScript:
    function getTheValue(id) {
    	return document.getElementById(id).value;
    }

    But that only works to get the value of the object, what if I wanted to access the class_Name or innerHTML

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    return document.getElementById(id)[the_property];
    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="^$">

  3. #3
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    return document.getElementById(id)[the_property];
    That did not work, am I supposed to call the function this way?

    Code JavaScript:
    getTheValue('field_name', 'value')

    or this way?

    Code JavaScript:
    getTheValue('field_name', '.value')

  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)
    Quote Originally Posted by tlacaelelrl View Post
    That did not work, am I supposed to call the function this way?

    Code JavaScript:
    getTheValue('field_name', 'value')
    Yes, that way.

    Here's a simple example of it in action:

    Code html4strict:
    <html>
    <head>
    </head>
    <body>
    <div id="somediv">
        <p>This is the contents of the div.</p>
    </div>
     
    <button id="getid">Get the ID</button>
    <button id="getinnerhtml">Get InnerHTML</button>
     
    <script>
    function getTheValue(id, the_property) {
        return document.getElementById(id)[the_property];
    }
     
    document.getElementById('getid').onclick =  function () {
        alert(getTheValue('somediv', 'id'));
    };
    document.getElementById('getinnerhtml').onclick =  function () {
        alert(getTheValue('somediv', 'innerHTML'));
    };
    </script>
    </body>
    </html>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I have no idea what was going on, I probably miss-typed something, when it did not work I undid the changes, then I waited for the response in this post, tried it again and now it works.


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
  •