JavaScript not working, need help

Discussion in 'Javascript' started by Manfred Kooistra, Jan 24, 2007.

  1. I have a problem with some JavaScript code not working. I'm sure I've
    done something obviously stupid, but I can't for the live of me figure
    it out. Can someone please help?

    This is the XHTML document (stripped to the relevant parts):

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <script type="text/javascript" src="switch.js"></script>
    </head>
    <body>
    <div id="one" style="display: block;">content</div>
    <div id="two" style="display: none;">content</div>
    <div><a href="#" onclick="switch(); return false;">Switch</a></div>
    </body>
    </html>

    and this is the JavaScript file switch.js:

    function switch() {
    document.getElementById('one').style.display = 'none';
    document.getElementById('two').style.display = 'block';
    }

    Can you see the problem?
     
    Manfred Kooistra, Jan 24, 2007
    #1
    1. Advertising

  2. Manfred Kooistra

    Evertjan. Guest

    Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:

    > I have a problem with some JavaScript code not working. I'm sure I've
    > done something obviously stupid, but I can't for the live of me figure
    > it out. Can someone please help?
    >
    > This is the XHTML document (stripped to the relevant parts):
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    > <head>
    > <script type="text/javascript" src="switch.js"></script>
    > </head>
    > <body>
    > <div id="one" style="display: block;">content</div>
    > <div id="two" style="display: none;">content</div>
    > <div><a href="#" onclick="switch(); return
    > false;">Switch</a></div>
    > </body>
    > </html>
    >
    > and this is the JavaScript file switch.js:
    >
    > function switch() {
    > document.getElementById('one').style.display = 'none';
    > document.getElementById('two').style.display = 'block';
    >}
    >
    > Can you see the problem?


    Do not start with a js in a separate file,
    you do not even know if it is loaded.
    Do not use tabs on usenet.
    Do not the XML crap that is not partinent to your Q.
    Do not say "not working" but show us the errorcode and errorline number.

    And you/we will see, that "switch" must be a reserved word.

    Try:

    function mySwitch() {



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 24, 2007
    #2
    1. Advertising

  3. Thanks, Evertjan. I changed the file to this:

    <html>
    <head>
    <script language="javascript" type="text/javascript">
    //<![CDATA[
    function mySwitch() {
    document.getElementById('one').style.display = 'none';
    document.getElementById('two').style.display = 'block';
    }
    //]]>
    </script>
    </head>
    <body>
    <div id="one" style="display: block;">content</div>
    <div id="two" style="display: none;">content</div>
    <div><a href="#" onclick="mySwitch(); return false;">Switch</a></div>
    <body>
    </html>

    There is no error message, it simply does nothing.
     
    Manfred Kooistra, Jan 24, 2007
    #3
  4. Sorry, there is an error code.

    I changed the file to HTML 4 Transitional with:

    <script language="javascript" type="text/javascript">
    <!--
    function mySwitch() {
    document.getElementById('one').style.display = 'none';
    document.getElementById('two').style.display = 'block';
    }
    // -->
    </script>

    and the error message tells me that "mySwitch is not defined" and point
    me to line 1, which reads: "<html>".

    Any ideas?
     
    Manfred Kooistra, Jan 24, 2007
    #4
  5. Changes everything back to XHTML, and now it works fine. Strange. I
    still have no idea, what the problem was ...
     
    Manfred Kooistra, Jan 24, 2007
    #5
  6. Manfred Kooistra wrote:

    > <div id="one" style="display: block;">content</div>
    > <div id="two" style="display: none;">content</div>


    > There is no error message, it simply does nothing.


    It does, but it's not obvious because both divs have the same content.
    Change the contents e.g.:

    <div id="one" style="display: block;">content 1</div>
    <div id="two" style="display: none;">content 2</div>

    and you'll see some difference.

    --
    Osmo
     
    Osmo Saarikumpu, Jan 24, 2007
    #6
  7. Manfred Kooistra

    Evertjan. Guest

    Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:

    > Thanks, Evertjan. I changed the file to this:


    [please always quote on usenet]

    > <html>
    > <head>
    > <script language="javascript" type="text/javascript">
    > //<![CDATA[


    Why this CDATA stuff?? Leave it out!!!!!!!!!

    > function mySwitch() {
    > document.getElementById('one').style.display = 'none';
    > document.getElementById('two').style.display = 'block';
    >}
    > //]]>
    > </script>
    > </head>
    > <body>
    > <div id="one" style="display: block;">content</div>
    > <div id="two" style="display: none;">content</div>
    > <div><a href="#" onclick="mySwitch(); return false;">Switch</a></div>
    > <body>
    > </html>
    >
    > There is no error message, it simply does nothing.
    >


    It would SHOW(!!!) nothing as "content" is the same as "content"!

    Try this:

    =========== test.html ================
    <div id="one" style="display: block;">content: 1</div>
    <div id="two" style="display: none;">content: 2</div>

    <a href="#" onclick="mySwitch(); return false;">Switch</a>

    <script type='text/javascript'>
    function mySwitch() {
    document.getElementById('one').style.display = 'none';
    document.getElementById('two').style.display = 'block';
    }
    </script>
    =======================================

    You see you do NOT need any header for this testing,
    at least in IE and FF.

    Preferably, IMHO, use a button,
    an anchor is for linking:

    <button onclick='mySwitch();'>Switch</button>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 24, 2007
    #7
  8. Manfred Kooistra

    Evertjan. Guest

    Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:

    > Sorry, there is an error code.


    > I changed the file to HTML 4 Transitional with:


    Thas sould have no influence.

    > <script language="javascript" type="text/javascript">


    Do not use language="javascript", it has been deprecated for many yesrs.

    > <!--


    Same for <!--


    > function mySwitch() {
    > document.getElementById('one').style.display = 'none';
    > document.getElementById('two').style.display = 'block';
    >}
    > // -->


    and for // -->

    > </script>
    >
    > and the error message tells me that "mySwitch is not defined" and point
    > me to line 1, which reads: "<html>".


    So, you have made an unrelated error in your code, Manfred.

    > Any ideas?
    >




    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 24, 2007
    #8
  9. Manfred Kooistra

    Evertjan. Guest

    Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:

    > Changes everything back to XHTML, and now it works fine. Strange. I
    > still have no idea, what the problem was ...
    >


    Again:

    1 Please always quote on usenet, this is not email,
    others want to read it too,
    and not all news servers are current with older mails of a thread.

    2 Please share the code [minimal] that maks the difference.
    Why make us guess?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 24, 2007
    #9
  10. On 24 Jan., 14:50, "Evertjan." <> wrote:
    > Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:
    >
    > > Changes everything back to XHTML, and now it works fine. Strange. I
    > > still have no idea, what the problem was ...

    >
    > Again:
    >
    > 1 Please always quote on usenet, this is not email,
    > others want to read it too,
    > and not all news servers are current with older mails of a thread.
    >
    > 2 Please share the code [minimal] that maks the difference.
    > Why make us guess?
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)


    The solution to my problem is what I posted in my original question.
    As I wrote: Now it works, I don't know why. There is no difference, so
    I thought there was no reason to post the same code again.
     
    Manfred Kooistra, Feb 3, 2007
    #10
  11. On 24 Jan., 14:37, Osmo Saarikumpu <> wrote:
    > Manfred Kooistra wrote:
    > > <div id="one" style="display: block;">content</div>
    > > <div id="two" style="display: none;">content</div>
    > > There is no error message, it simply does nothing.

    >
    > It does, but it's not obvious because both divs have the same content.
    > Change the contents e.g.:
    >
    > <div id="one" style="display: block;">content 1</div>
    > <div id="two" style="display: none;">content 2</div>
    >
    > and you'll see some difference.
    >
    > --
    > Osmo


    In my file the two divs did not contain the same "content" and
    "content" [that was an error here in my posts], but images in one div
    and text in the other - so the switching or non-switching was
    unmistakeable.
     
    Manfred Kooistra, Feb 3, 2007
    #11
  12. Manfred Kooistra

    kim mizell Guest

    kim mizell, Feb 17, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Catherine Jones

    Need help! Sample not working in frm 1.1

    Catherine Jones, Dec 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    567
    Catherine Jones
    Dec 24, 2003
  2. MSFT
    Replies:
    11
    Views:
    598
    Grimsa
    Dec 3, 2003
  3. JeffQ888
    Replies:
    0
    Views:
    203
    JeffQ888
    May 23, 2006
  4. Jarrod Hyder

    Need a javascript guru (I need help!)

    Jarrod Hyder, Jan 27, 2005, in forum: Javascript
    Replies:
    1
    Views:
    90
  5. Wiseman
    Replies:
    1
    Views:
    452
    Henry
    Jul 25, 2008
Loading...

Share This Page