Results 1 to 19 of 19

Thread: I hate divs and javascript [Resolved]

  1. #1

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    I hate divs and javascript [Resolved]

    Everytime I do this, it doesn't work. All I'm trying to do is toggle between 2 divs.

    Script:
    Code:
    function toggle() { 
    // DOM3 = IE5, NS6 
    document.getElementById('div1').style.visibility = 'visible'; 
    document.getElementById('div2').style.visibility = 'hidden'; 
    } 
    
    function toggle2() { 
    // DOM3 = IE5, NS6 
    document.getElementById('div1').style.visibility = 'hidden'; 
    document.getElementById('div2').style.visibility = 'visible'; 
    }
    Call to them:
    PHP Code:
    echo "<br><br><input type=radio name=opt value=1 onclick='toggle()'>Specify Test";
            echo 
    "<input type=radio name=opt value=1 onclick='toggle2()'>Specify Cell<br>";
            echo 
    "<div id=div1 style=visibility:visible>testtesttesttest</div>";
            echo 
    "<div id=div2 style=visibility:hidden>test2test2test2test2test2</div>"
    IT WON'T WORK AND I MIGHT PULL MY HAIR OUT.
    Last edited by ober0330; Jul 23rd, 2004 at 08:16 AM.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    PHP Code:

    echo "<br><br><input type='radio' name='opt' value=1 onclick='toggle()'>Specify Test";
            echo 
    "<input type='radio' name='opt' value=1 onclick='toggle2()'>Specify Cell<br>";
            echo 
    "<div id='div1' style='visibility:visible;'>testtesttesttest</div>";
            echo 
    "<div id='div2' style='visibility:hidden;'>test2test2test2test2test2</div>"
    There. Now you've got correct HTML.

    I didn't test it at all, but try it now. I'd also re-wriet the JS code so that you only need one function.
    Code:
    //untested
    function toggle_div() {
    d1= document.getElementByID('div1')
    d2= document.getElementByID('div2')
    if (d1.style.visibility == 'visible')
        {
        d1.style.visibility = 'hidden'
        d2.style.visibility = 'visible'
        }
    else
        {
        d2.style.visibility = 'hidden'
        d1.style.visibility = 'visible'
        }
    }
    just thought of something, toggle might be a keyword, change the name of the function. Also, what is the error you get?
    Have I helped you? Please Rate my posts.

  3. #3

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    No dice

    It doesn't give me an error. The only thing it does when I click the other option is the status bar says "Script Error!" for a brief second. Is there a way to get some sort of JS debugger?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  4. #4
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    urgh. IE.
    In IE you can double click on that little yellow triangle for a slightly better error report.
    Have I helped you? Please Rate my posts.

  5. #5

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Well, I'm mainly using Opera, but that doesn't give me anything. And the other browser is MyIE2, which doesn't give me the triangle.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  6. #6
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Use Firefox.
    OK, here's what I'd do. Shove all the JS in an external file, then try to validate your page. Once the HTML is valid, then the JS should definately work.

    Note that since you're using PHP you might have to upload the file before you can validate it as otherwise there is no server to run the PHP.
    Have I helped you? Please Rate my posts.

  7. #7

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    The JS is already in an external file.

    edit: It won't validate a php file.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  8. #8
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    good, then just make it validate.
    Have I helped you? Please Rate my posts.

  9. #9

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    In case you missed it, that validator does not work with php files.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  10. #10

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH!!!!!!!

    Ok, here's a test file:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    
    <html>
    <head>
    	<title>Untitled</title>
        <SCRIPT type="text/javascript" SRC="../js/toggle2.js"></SCRIPT>
    </head>
    
    <body>
    <br><br><input type='radio' name='opt' onclick='tog_div'>Specify Test
    <input type='radio' name='opt' value='test' onclick='tog_div'>Specify Cell<br>
    <div id='div1' style='visibility:visible'>testtesttesttest</div>
    <div id='div2' style='visibility:hidden'>test2test2test2test2test2</div>
    
    
    </body>
    </html>
    The contents of the js file are the function you gave me, only with a slight name change.

    The file validates as good html. When I run it in IE and the event fires it says "Object expected on line 10", which is the line right after the body.

    Any ideas??
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by ober0330
    In case you missed it, that validator does not work with php files.
    You hear that? That's the BS alarm.... IT will validate PHP files, trust me.... it does an HTTP GET which causes the server to run the PHP and serve up the resulting HTML....

    But, that asside... your HTML still has some problems....
    You open your input tags, but never close them, which means you end up with two div tags inside an input tag, which is then inside the other input tag! That's what the javascript error is.... because of the nested tags, the div tags aren't where they should be.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What it won't do is to accept PHP files you directly upload to the validator.

    techgnome, in HTML you can't and need not close <input> and similar tags (br, img, param, embed...)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    getElementById. small letter d

  14. #14

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Originally posted by techgnome
    You hear that? That's the BS alarm.... IT will validate PHP files, trust me.... it does an HTTP GET which causes the server to run the PHP and serve up the resulting HTML....

    But, that asside... your HTML still has some problems....
    You open your input tags, but never close them, which means you end up with two div tags inside an input tag, which is then inside the other input tag! That's what the javascript error is.... because of the nested tags, the div tags aren't where they should be.

    TG
    Echo CornedBee. I uploaded the file and it couldn't handle it. I can't use the URL to the site because it's an intranet site.

    And where did you learn HTML? There are no closing tags for input!
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  15. #15

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Ok... oddly, it didn't like the fact that I was linking to the js file. I copied the script into the page and now it works. All that work for THAT!?

    Anyways... it's over.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Hmm... path problem? Sounds like the js file wasn't loaded.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    I don't know... I checked the path and everything. It all seemed to be fine and I've used this method on other pages where it worked great. I don't know... it's a small function and it'll only go on this page probably, so I guess I'll just leave it.

    I don't know why it wasn't loading it tho... and that bothers me.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  18. #18
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    I used to make the mistake of putting <script></script> onto the .js file as well as in the HTML document. you didn'y do that did you?
    Have I helped you? Please Rate my posts.

  19. #19

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Thanks for the tip.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

Posting Permissions

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



Click Here to Expand Forum to Full Width