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:

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

And this:

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:


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

return document.getElementById(id)[the_property];

That did not work, am I supposed to call the function this way?

getTheValue('field_name', 'value')

or this way?

getTheValue('field_name', '.value')

Yes, that way.

Here’s a simple example of it in action:


<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>

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.