novice
novice

Reputation: 111

style not being applied to object

I have the following function in my JavaScript file:

 function menu_display(a) {
  document.getElementsById("dropmenu2").style.visibility="visible";
   }

The problem is the style is not being applied on my object (the function is getting called, I checked that using an alert). In fact, the only suggestions by Microsoft visual web developer for this object are: constructor, toString, valueOf, isProtypeOf and a few more. It is not even suggesting that 'style' can be applied. What is the problem?

After fixing getElementsById by replacing with getElementById, the function works fine when I place it in my html file, but if I place it in an external JavaScript file, then the function is getting called (I checked that), but yet again style is not one of the suggestions. This time there are no spelling mistakes.

Upvotes: 1

Views: 133

Answers (3)

jmoerdyk
jmoerdyk

Reputation: 5520

There is no getElementsById() is not a valid JS function. The proper function is getElementById(), singular, not plural, because the HTML spec only allows a single element on a page to have a particular ID. Element IDs must be unique.

Upvotes: 1

Paul
Paul

Reputation: 141839

You have two typos:

Elements should be Element in getElementById.
syle should be style.

 document.getElementById("dropmenu2").style.visibility = "visible";

Upvotes: 0

James Hill
James Hill

Reputation: 61792

getElementsById() is not a function of document (or of anything). You're looking for getElementById().

document.getElementById("dropmenu2").style.visibility = "visible";

Check it out on MDN

Upvotes: 3

Related Questions