runtime error 'function undefined'

Discussion in 'JavaScript' started by JxnNC, Dec 27, 2011.

  1. #1
    Im trying to make it so that in a div tag different search fields appear on click of a link in a menu and only one form shows at a time. function toggleDisplay(id) { document.getElementById(id).style.visibility = "visible"; if (document.getElementById(id).style.display == "none") { document.getElementById(id).style.display = ""; ) else { document.getElementById(id).style.display = "none"; ) }its saying 'toggleDisplay' is undefined
     
    JxnNC, Dec 27, 2011 IP
  2. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #2
    JxnNC,

    You are using ")" instead of "}" few times. Fixing those typos should resolve your issue.
     
    2WDH.com, Dec 28, 2011 IP
  3. JxnNC

    JxnNC Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    function toggleDisplay(id) { document.getElementById(id).style.visibility = "visible"; if (document.getElementById(id).style.display == "none") { document.getElementById(id).style.display = ""; } else { document.getElementById(id).style.display = "none"; } }This returns the same error...
     
    JxnNC, Dec 28, 2011 IP
  4. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #4
    That code is fine.
    Here is a sample that works with the mentioned code (verified):
    <script>
    function toggleDisplay(id) {
        document.getElementById(id).style.visibility = "visible";
        if (document.getElementById(id).style.display == "none") {
            document.getElementById(id).style.display = "";
        } else {
            document.getElementById(id).style.display = "none";
        }
    }
    </script>
    <div id="testDiv">Content</div>
    <button onClick="toggleDisplay('testDiv');">Show/Hide</button>
    Code (markup):
    Maybe you have a typo in the function call part? Can you show the whole code or post URL to your page?
     
    2WDH.com, Dec 28, 2011 IP
  5. Basti

    Basti Active Member

    Messages:
    625
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    90
    #5
    Try this one

    
    function toggleDisplay(id) {
    
      var togglediv = document.getElementById(id);
    
      if (togglediv.style.display == "none") {
       togglediv.style.display = "block";
      }
      else {
        togglediv.style.display = "none";
      }
    
    }
    
    Code (markup):
    On the link use
    
    <a href="#" onClick="toggleDisplay('divid'); return false;">Show/hide searchbox</a>
    
    Code (markup):
    toggleDisplay('divid'); in there (divid ) replace with the div id of your search form to have it show/hide upon click.

    You might also want to consider using jquery, i find it a lot more useful, with less writing
     
    Basti, Dec 28, 2011 IP
  6. JxnNC

    JxnNC Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Im calling it with this Basic and copy and pasted what you posted and linked it by putting it in the content placeholder of an asp page in visual studio2010, and i put the code in a javascript folder...
     
    JxnNC, Dec 28, 2011 IP
  7. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #7
    Please post the following parts of your code:
    1. Contents of your .js file in javascript folder
    2. Part of your HTML code where you include the mentioned .js file
    2. Part of your HTML code where you are using toggleDisplay function
     
    2WDH.com, Dec 28, 2011 IP
  8. JxnNC

    JxnNC Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This is whats in js folder
    -----//<script language="javascript" type="text/javascript">

    function toggleDisplay(id) {
    document.getElementById(id).style.visibility = "visible";
    if (document.getElementById(id).style.display == "none") {
    document.getElementById(id).style.display = "";
    } else {
    document.getElementById(id).style.display = "none";
    }
    }

    </script>-----.//
     
    JxnNC, Dec 28, 2011 IP
  9. JxnNC

    JxnNC Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This is how i put it in html--//<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script src="js/JScript1.js" type="text/javascript"></script>
    </asp:Content>--//
     
    JxnNC, Dec 28, 2011 IP
  10. JxnNC

    JxnNC Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    This is how I called it ---///<button onClick="toggleDisplay('searchBasic');">Basic</button> ---///
     
    JxnNC, Dec 28, 2011 IP
  11. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #11
    Remove the "<script language="javascript" type="text/javascript">" and "</script>" lines from your js/JScript1.js file and check if everything works then.
     
    2WDH.com, Dec 28, 2011 IP
  12. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #12
    The RED lines do not belong in a .js file. Get rid of them.
     
    mmerlinn, Dec 31, 2011 IP
  13. JxnNC

    JxnNC Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Well, it stopped the runtime error, but now, its just not changing the space like I wanted it to. Were about to start using Kendo UI for this part though I believe, I guess I am curious as to why it was not functioning properly anyway, but thank you for all your help
     
    JxnNC, Dec 31, 2011 IP