www.webdeveloper.com
Results 1 to 5 of 5

Thread: functions

  1. #1
    Join Date
    Apr 2012
    Posts
    110

    functions

    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?

  2. #2
    Join Date
    Mar 2011
    Posts
    1,139
    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.
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Apr 2012
    Posts
    110
    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...)

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    why you don't like functions that much?
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    Mar 2011
    Posts
    1,139
    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!
    Rick Trethewey
    Rainbo Design

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles