1
function scrollfun()
{
    var ob = document.getElementById('rowScroll');
    ob.style.height ="300px";
    ob.style.width = "200px";               
}

Here rowScroll is the <div> id. The above code is working properly. But the following code is not working:

function scrollfun()
{
    var ob = document.getElementById('rowScroll');
    ob.style.height ="30%";
    ob.style.width = "20%";             
}

How can I get this working with percentages?

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
user1429962
  • 151
  • 1
  • 3
  • 9
  • Percents work the same way as do px work. – Ehs4n Jan 31 '13 at 06:48
  • *'Not working'* doesn't say anything about the problem; in general, explain what you are expecting to happen, and what is actually happening. In this case, the latter is probably sufficient. – nbrooks Jan 31 '13 at 06:55
  • can I ask why you are doing this with a JS function and not using CSS ? – SQLGuru Jan 31 '13 at 06:55

4 Answers4

0

You will need to use a div tag to achieve this. See the working code below:

<!DOCTYPE html>
<html>
<head>
<script>
function displayResult()
{
document.getElementById("b1").style.height="200%";
}
</script>
</head>
<body>

<div style="height:25px;">
<input type="button" id="b1" onclick="displayResult()" value="Change height of button" >

</body>
</html>
Mayur Manani
  • 795
  • 4
  • 12
0

What if you "tell" it what are these percentages from. like

function scrollfun()
{
    var ob = document.getElementById('rowScroll');
    ob.style.height = (container-width * 30)/100 + "px"
    ob.style.width = (container-width * 20)/100 + "px"             
}
0

FYI percentage based width height depends upon parent container of your element 'rowScroll'. Try giving height, width info to the parent itself...

UPDATE:

for making height width 50% of screen following will do:

function windowHW() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myHeight,myWidth];
}
function scrollfun()
    {
        var ob = document.getElementById('rowScroll');
        document_dimension = windowHW();
        ob.style.height =document_dimension[0]/2;
        ob.style.width =document_dimension[1]/2;
    }

Please note that function windowHW is slight modification of solution given in:

stackoverflow question: JavaScript - Get Browser Height answered by meder

Community
  • 1
  • 1
  • height of the div should be 50% or 60% of height of the screen or window – user1429962 Jan 31 '13 at 07:16
  • This is not an answer. To request additional information or suggest a modification, post a *comment* below the original post. The *Answer* section, is reserved for answers (or at least attempts to answer) the problem posed in the question. – nbrooks Jan 31 '13 at 07:30
  • i kept that as an answer : 'percentage based width height depends upon parent container of your element 'rowScroll'. Try giving height, width info to the parent itself.' – Mrigesh Raj Shrestha Jan 31 '13 at 07:31
0

just use the property className from javascript

function scrollfun()
{
   document.getElementById("rowScroll").className = "MyClass";      
}

And in your css

.MyClass{height:30%;width:20%;}
nbrooks
  • 18,126
  • 5
  • 54
  • 66
Krish
  • 2,590
  • 8
  • 42
  • 62