Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Javascript function works only the first time it's called

Javascript function works only the first time it's called

Scheduled Pinned Locked Moved Web Development
helpjavascriptsysadminannouncement
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bjohnson33
    wrote on last edited by
    #1

    Hi Hope someone can help with this as I'm feeling a bit stupid at the moment! I've got a form (using a master page and an update panel in case it's relevant). This is also (and unfortunately needs to) running in IE6. On there are 3 text boxes which are pre-populated server side. When the value of one of these changes, I need to update another part of the screen (client-side, this doesn't need to go back to the server). So, each of those text boxes has:onFocus="storeValue(this)" onBlur="leavingField(this) so that on entry to the field the value is stored in a global variable and on exit this is checked and actioned if it has changed. The first time you enter and exit a field it behaves as expected, subsequently you get a 'Function Expected' error. The relevant code is: var valStore; function storeValue(field) { valStore = field.value; } function hasValueChanged(val) { if (val != valStore) hasValueChanged = true; else hasValueChanged = false; } function leavingField(field) { if (hasValueChanged(field.value)) { var prefix = (ctlPrefix + curr); calculateSingleDifference(field.id.split(prefix)[1]); field.className = (field.className + ' changedValue'); } } The error is raised the second time 'hasValueChanged' is called in 'leavingField'. Any ideas gratefully received! Thanks in advance Ben

    Y 1 Reply Last reply
    0
    • B Bjohnson33

      Hi Hope someone can help with this as I'm feeling a bit stupid at the moment! I've got a form (using a master page and an update panel in case it's relevant). This is also (and unfortunately needs to) running in IE6. On there are 3 text boxes which are pre-populated server side. When the value of one of these changes, I need to update another part of the screen (client-side, this doesn't need to go back to the server). So, each of those text boxes has:onFocus="storeValue(this)" onBlur="leavingField(this) so that on entry to the field the value is stored in a global variable and on exit this is checked and actioned if it has changed. The first time you enter and exit a field it behaves as expected, subsequently you get a 'Function Expected' error. The relevant code is: var valStore; function storeValue(field) { valStore = field.value; } function hasValueChanged(val) { if (val != valStore) hasValueChanged = true; else hasValueChanged = false; } function leavingField(field) { if (hasValueChanged(field.value)) { var prefix = (ctlPrefix + curr); calculateSingleDifference(field.id.split(prefix)[1]); field.className = (field.className + ' changedValue'); } } The error is raised the second time 'hasValueChanged' is called in 'leavingField'. Any ideas gratefully received! Thanks in advance Ben

      Y Offline
      Y Offline
      Yusuf
      wrote on last edited by
      #2

      Bjohnson33 wrote:

      function hasValueChanged(val) { if (val != valStore) hasValueChanged = true; else hasValueChanged = false; }

      well, to start with you got hasValueChanged both as function name and variable name. I can see where that originrated :doh: try this for a size, function hasValueChanged(val) { if (val != valStore) return true; else return false; }

      Yusuf May I help you?

      B 1 Reply Last reply
      0
      • Y Yusuf

        Bjohnson33 wrote:

        function hasValueChanged(val) { if (val != valStore) hasValueChanged = true; else hasValueChanged = false; }

        well, to start with you got hasValueChanged both as function name and variable name. I can see where that originrated :doh: try this for a size, function hasValueChanged(val) { if (val != valStore) return true; else return false; }

        Yusuf May I help you?

        B Offline
        B Offline
        Bjohnson33
        wrote on last edited by
        #3

        Thanks Yusuf, exactly right - was obviously having a syntax meltdown - fifth language this week! Ben

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups