Please tell me why javascript may not work in Firefox if mandatory in my opinion is present

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

The script is such and it is characteristic that it is very simple.

 function myFunc(elem1, elem2, elem3) { elem1.style.display = 'block'; elem2.style.display = 'none'; elem3.style.display = 'none'; } 

called as follows

 onclick = "myFunc(e1, e2, e3);" 

What does he need XD)

Put the code ...

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <script> function myFunc(elem1, elem2, elem3) { elem1.style.display = 'block'; elem2.style.display = 'none'; elem3.style.display = 'none'; } </script> </head> <body> <div id="e3">Previous</div> <input onclick="myFunc(e1, e2, e3)" type="submit" id="send" value="getE1" /> <input onclick="myFunc(e2, e1, e3)" type="submit" id="send" value="getE2" /> <div id="e1" style="display:none;">e1</div> <div id="e2" style="display:none;">e2</div> </body> </html> 
  • Onclick, as I understand it, do you write in the very body of the page? O_o <br> If so, the question arises - what are e1, e2, e3 in this case equal to? ^^ <br> PS: where is the DOCTYPE then? <br> PPS: if I misunderstood you - please post the entire code ... - Zowie
  • If you remove the DOCTYPE line, then everything is ok! e1, e2, e3 are id as in the body of the page to write onclick yet? so triple? onclick = "myFunc (e1, e2, e3);" The script is given for example and not to clarify the details! - Palmervan
  • You can put all the code code or not? <br> If it were a trifle, I would not ask, it did not occur to you? Just a post from the discharge - here I have the code and there is such a mistake, the code goes something like this, what's the matter ^^ <small> [offtop] well, you have a mess in my head ... [/ offtop] </ small> - Zowie
  • If you use the DOCTYPE in this way <! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.0 Transitional // EN" " w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> Then everything is fine! So I wonder why this is how it all comes out! - Palmervan
  • Why does it work, I generally do not understand, the point is that the code is illogical and terrible ... actually start already using firebug, many questions will disappear by themselves, the code below works with any doctype ... - Zowie

1 answer 1

Well, first we pass the string and not the variable, i.e. you need to write not myFunc(e2, e1, e3) , but myFunc('e2', 'e1', 'e3') , I hope this is understandable ...
Well, in general:

 function myFunc(elem1, elem2, elem3) { //сначало выберем еллементы, не?.. elem1 = document.getElementById(elem1); elem2 = document.getElementById(elem2); elem3 = document.getElementById(elem3); //ну а теперь уже мы можем играться со стилями... elem1.style.display = 'block'; elem2.style.display = 'none'; elem3.style.display = 'none'; } 

Now I hope that doctype has nothing to do with it? :)

  • Accepted! But the crux of the matter was why it is not working in FF! The IE6 example I gave is hawking ) - Palmervan