-3

Any reason why this javascript won't run?

<script>
  alert(window.screen.availHeight+"px");
  var height = window.screen.availHeight + “px”;
  document.getElementsByClassName(“col-sm-9”).style.height = height;
</script>
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
Omkar Dixit
  • 746
  • 1
  • 9
  • 19
  • 1
    `Java` and `Javascript` are two different languages. Have you checked the browser console for errors? If not then I recommend you start there. Also `javascript` will not recognise smart quotes. On top of that, your selector `getElementsByClassName` will return a node list/collection of elements so you need to specify which one you want to target. – NewToJS Sep 07 '18 at 19:38
  • I am using this script in my html content, but i don't get the alert msg i have written – Omkar Dixit Sep 07 '18 at 19:40
  • Do you really have curly quotes in the script, or is that a copying error? – Barmar Sep 07 '18 at 20:22
  • Are you seeing the alert? – Barmar Sep 07 '18 at 20:24

1 Answers1

0
document.getElementsByClassName('col-sm-9').style.height

the getElementsByClassName method returns an array. (hint: Elements not element)

You either need to select the first element: document.getElementsByClassName('col-sm-9')[0].style.height = height

Or iterate though the array e.g:

for(var i=0; i<document.getElementsByClassName('col-sm-9').length; i++)
{
    document.getElementsByClassName('col-sm-9')[i].style.height = height;
}
dustytrash
  • 1,568
  • 1
  • 10
  • 17