1

I have one div content whose height should be 300px when i click on another div name button. But how can i reset the height, when again clicked on button div?

Here is the javascript code for reference:

function chk()
{
    var x = document.getElementById('content').style.height = '300px';

}

This is the HTML code

<div id="content">
This is dummy text.
    </div>
    <div id="button" onclick="chk()">
    click to read
    </div>

When button div is click content height increases, but how can i reduce the height by clicking on same div with same onclick event?

5
  • @akonsu can you please show me how i can do that? Commented Dec 2, 2013 at 23:33
  • 1
    You can just add a css rule as well... jsfiddle.net/Nhcq3 Commented Dec 2, 2013 at 23:35
  • @PSL that is how i want it to work. But transition property not working. if you can put that too. that would be great. Commented Dec 3, 2013 at 0:00
  • @SahibjotSingh You can just add a css transition to the rule something like this jsfiddle.net/9k9M3 Commented Dec 3, 2013 at 0:05
  • @PSL that is not going smoothly. It is working but it should smoothly increase and decrease like transition-duration property. Commented Dec 3, 2013 at 0:16

4 Answers 4

2

Use CSS:

#content {
    height: auto;
}
#content.expand {
    height: 300px;
}

In your script:

function chk()
{
    var node = document.getElementById('content');

    node.classList.toggle('expand');
}

This keeps the state local to the element you're working on, which makes for more flexible code.

See also: classList API

Sign up to request clarification or add additional context in comments.

4 Comments

that is how i want it to work. But transition property not working. if you can put that too. that would be great.
@SahibjotSingh What transition property?
transition-duration. So that height increases and decreases smoothly.
@SahibjotSingh This question is about solving a JavaScript issue, so in order to get an answer regarding CSS transitions I would suggest either searching for that or ask another question.
2

You could use a flag:

var isSet = false:

function chk(){
    if(!isSet) {
        var x = document.getElementById('content').style.height = '300px';
        isSet = true;
    }
    else {
        // some other computation
        isSet = false;
    }

}

Comments

2

Either a flag

var flag;

function chk() {
    var height = flag ? '0px' : '300px';
    document.getElementById('content').style.height = height;
    flag = !flag;
}

or by checking the current height

function chk() {
    var currHeight = document.getElementById('content').style.height;
    var setHeight = height == '300px' ? '0px' : '300px';
    document.getElementById('content').style.height = setHeight;
}

Comments

0

If you are just learning HTML, CSS, and JavaScript you should start by making your code more clear.

// HTML should look more like
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  <head>
    <meta http-equiv='content-type' content='text/html;charset=utf-8' />
    <style type='text/css'>
      @import 'common.css'; @import 'page.css';
    </style>
  </head>
<body>
  <div id='content'>
    This is dummy text.
  </div>
  <input type='button' value='click to read' id='button' />
  <script type='text/javascript' src='common.js'></script>
  <script type='text/javascript' src='page.js'></script>
</body>
</html>

Notice, your button should be a button, not a div. XHTML is more expandable for scraping and XSLT, and will work on HTML pages, but not the other way around.

// learn to keep your JavaScript separate pages for caching - common.js
//<![CDATA[
// reduce objects to smaller variables and never use `document.getElementById()`
var doc = document, bod = doc.body;
function E(e){
  return doc.getElementById(e);
}
//]]>

// page.js
//<![CDATA[
var button = E('button');
/* The self-executing Anonymous Function below creates scope for `var test` and
   returns an unexecuted function you can call later. Note that a function is 
   basically a variable that if you add `()` to will execute. */
var changeHeight = (function(){
  var test = false;
  return function(id, before, after){
    E(id).style.height = test ? before : after;
    test = !test;
  }
})();
/* This is a backward compatible way of creating an `onclick` function, unlike
   `.addEventListener()` and `attachEvent()`, this is assignment, so it will
    write over your last `onclick` assiged to this specific Element */
button.onclick = function(){
  changeHeight('content', '20px', '300px');
}
// To combat the comment above you could do something like this:
/*
button.onclick = function(){
  changeHeight('content', '20px', '300px');
}
function anotherFunction(){
  console.log('wow');
}
var before = button.onclick;
button.onclick = function(){
  if(before)before();
  anotherFunction();
}
*/
/* An executed function, would execute before the event is handled. The only
   thing that is automatically passed to your variable function or Anonymous
   Function is the Event Object. In this case it is not necessary, because we
   are not accessing the Event Object. */
//]]>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.