www.webdeveloper.com
Results 1 to 3 of 3

Thread: If Else Width

  1. #1
    Join Date
    Nov 2013
    Posts
    2

    If Else Width

    Hello, Java Script Newb here. I'm more of an interaction designer than a programmer, but I'm working on a prototype for an iPad app...

    Can anyone post basic java script code that will make a div grow and shrink when you click on it using an if / else statement?

    This is what I've been using but it won't work:


    <html>
    <head>
    <style>

    #coolDiv{
    background-color: #586e75;
    width: 150px;
    height: 75px;
    }

    </style>
    <script>

    var box;

    function coolFunction(){
    box=document.getElementById("coolDiv")
    if (box.style.width.match("150px"))
    {
    box.style.width="500px";
    } else {
    box.style.width="150px";
    }

    </script>
    </head>
    <body>

    <div id="coolDiv" onclick="coolFunction()"></Div>

    </body>
    <html>

  2. #2
    Join Date
    Nov 2013
    Posts
    2
    Curly bracket after if / else statement.

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <style>
    #coolDiv{background-color:#586e75;width:150px;height:75px;cursor:pointer;}
    </style>
    <script>
    function doc(id){return document.getElementById(id);}
    
    var box,wd,max=500,min=150,dir='up';
    
    function grow(){
    doc('coolDiv').onclick=function(){return false};
    var limit=(dir==='up')?max:min;
    box=doc('coolDiv'),wd=box.offsetWidth;
    if(wd != limit){
    dir==='up'?(wd++):(wd--);
    box.style.width=wd+'px';
    setTimeout('grow()',3);
    }
    else{
    dir==='up'?(dir='down'):(dir='up');
    doc('coolDiv').onclick=grow;
    return;
    }
    }
    
    window.onload=function(){doc('coolDiv').onclick=grow;}
    </script>
    </head>
    <body>
    <div id="coolDiv"></div>
    </body>
    <html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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