I am trying to change content in a pane using javascript. If i try a funciton, and call on it via an action (button click) it does what i want. if i take away the funciton part, and just leave the .innerHTML='something' then it does nothing. what is the difference? am I missing something?
The problem you're having is most likely due to the timing of when your JavaScript gets executed. JavaScript not in a function gets executed as the browser encounters it within the document. So if your naked JavaScript resides before/above the element you want to change, the script can't find the element in order to change it because it doesn't exist yet. But your onclick() function gets called after the document is fully loaded and rendered, so it works as you expect it will.
so if i have the div after the java code, it is not able to find it, can cannot do anything. so what i should do then, is put the javascript code after the div? (or always just make it a function...)
The basic answer to your question is yes, put the JavaScript code in your document after the elements you want it to work with. But, generally, it's best practice to avoid embedded JavaScript unless you have a compelling reason to use it. And if the point is to have the page change as a result of a user's action, then you can't use embedded JavaScript anyway. You'll need to use a function. Good luck!
Bookmarks