Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  21] [ 6]  / answers: 1 / hits: 25814  / 11 Years ago, sat, december 7, 2013, 12:00:00

I want to increase the height of the div tag on click of button. Every time a user clicks a button it should increase the height of that particular div tag, say by 200px or so..



HTML



      <div id=controls>
<input type=button onclick=incHeight() id=btn name=btn>
</div>
<div id=container style=min-height:250px;> &nbsp;</div>


The below script works properly



Javascript



      <script type=text/javascript>
function incHeight()
{
document.getElementById(container).style.height = 250+'px';

}
</script>


But I want to do something like this, which is not working. The problem I think is the 'px' portion in the value. Anybody have any idea how to extract the INT portion of the value...



      <script type=text/javascript>
function incHeight()
{
document.getElementById(container).style.height += 250;
}
</script>


The problem is how do I get the '250' portion of the height value neglecting the 'px' in javascript..


More From » html

 Answers
31

Try this:



function incHeight() {
var el = document.getElementById(container);
var height = el.offsetHeight;
var newHeight = height + 200;
el.style.height = newHeight + 'px';
}


Fiddle


[#73865] Thursday, December 5, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
paolam

Total Points: 437
Total Questions: 107
Total Answers: 106

Location: Aland Islands
Member since Wed, Nov 17, 2021
3 Years ago
paolam questions
Thu, Oct 29, 20, 00:00, 4 Years ago
Wed, Nov 20, 19, 00:00, 5 Years ago
;