cyclobs
cyclobs

Reputation: 45

javascript "document.getElementById undefined"

i dunno but i might be missing something, i've tried a few thigns but they all end up with the same result and i'm about to start pulling my hair out

<script type="text/javascript">
function set_size() {
// Get users Screen Res
var screen_height = screen.height;
var attribute = "height";
var element = "pdfFile";

// Take say 200px off the res to accumulate the browser screen useage
var object_height = screen_height - 200;

// Write to the height attribute
document.getElementById[element].getAttribute[attribute].value = object_height;
document.getElementById['jsoutput'].innerhtml = object_height;
}
</script>

<div class="Contact_list">
<a name="contacts"><object id="pdfFile" data="files/contacts/pdf/contacts.pdf" type="application/pdf" width="100%" height="600" style="border: 1px solid;"></object></a>
<span id="jsoutput"></span>
</div>

<script type="text/javascript">
set_size();
</script>

[12:13:28.050] document.getElementById[element] is undefined @ http://localhost/**/index.php?page=contacts#contacts:57

Upvotes: 3

Views: 8296

Answers (4)

jfriend00
jfriend00

Reputation: 707238

Both of these should not be like this:

// Write to the height attribute
document.getElementById[element].getAttribute[attribute].value = object_height;
document.getElementById['jsoutput'].innerhtml = object_height;

but should be like this:

// Write to the height attribute
document.getElementById(element).style.height = object_height;
document.getElementById('jsoutput').style.height = object_height;

getElementById and getAttribute are functions whose parameters goes in parentheses, not in brackets.

Height is set by setting style.height, not setting .value on an attribute. You generally do NOT set the height on an inline element which a <span> tag is. You can set the height on a block element or an inline-block element. So, your attempt to set the height on the jsoutput object will probably not do what you want.

Also, the innerHTML attribute must be capitalized as innerHTML, not innerhtml, though you weren't using that attribute correctly anyway so I replaced it with style.height.

Upvotes: 4

cocoafan
cocoafan

Reputation: 4894

you must use parenthesis like ( and ) since getElementById is a function and not an object.

document.getElementById(element)

Upvotes: 0

Dinash
Dinash

Reputation: 3047

getElementById(element)notdocument.getElementById[element]`.. please verify that...

Upvotes: 0

Ivan Peevski
Ivan Peevski

Reputation: 963

getElementById is a function, not an array - call it with () instead of [] around the ID

Upvotes: 2

Related Questions