1

I want to access class"pdocCover" for set width and height of body by javaScript . How should I do?.

<?xml version="1.0" encoding="utf-8"?>
<!-- Generated by PubliForge, $Date: 2012/02/03 12:17:54 $ -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="Content-Language" content="en"/>
<title>Couverture</title>
<link rel="StyleSheet" href="Css/reset.css" type="text/css"/>
<link rel="StyleSheet" href="Css/publidoc.css" type="text/css"/>
<link rel="StyleSheet" href="Css/main.css" type="text/css"/>
</head>
<body class="pdocCover">
<div>
  <img src="Images/9782919504060.png" alt="Couverture"/>
</div>
</body>
</html>
Dashzaki
  • 568
  • 2
  • 6
  • 13
  • Duplicate : http://stackoverflow.com/questions/1933602/how-to-getelementbyclass-instead-of-getelementbyid-with-javascript – Pranav 웃 Jun 21 '12 at 04:13

3 Answers3

1

By using jQuery

$(".pdocCover").css("height","500px");
$(".pdocCover").css("width","500px");

By using JavaScript

document.getElementsByClassName('pdocCover').style.height="500px;"
document.getElementsByClassName('pdocCover').style.width="500px;"
Sudip Pal
  • 2,041
  • 1
  • 13
  • 16
  • 1
    `getElementsByClassName` Only works in IE9 and none of the previous IEs. http://www.quirksmode.org/dom/w3c_core.html#t11 – Trufa Jun 21 '12 at 04:25
  • @Trufa I use it to access class in iOS App .Thank you for your answer. ^^ – Dashzaki Jun 21 '12 at 04:29
  • `getElementsByClassName`returns a [NodeList](https://developer.mozilla.org/En/DOM/NodeList), similar to an Array, so the `style` property is undefined. You'll _probably_ want to, in this case, pull out the first element or iterate through them all. – Cecchi Jun 21 '12 at 04:38
1

To achieve what you specifically described:

document.getElementsByClassName('pdocCover')[0].width = '100px'

... but that probably isn't what you want. For one you can access the body more quickly and clearly by doing:

document.getElementsByTagName('body')[0]

and this will not change the width or height of the window itself if that is what you are attempting to do. Unless you create a new window with window.open you do not have control of the window size.

Cecchi
  • 1,525
  • 9
  • 9
0

document.getElementByClassName("Classname") should return array of object who have class "classname". choose your one by selecting the index: [0] the first element, [1] the second etc.

nimrod
  • 132
  • 1
  • 1
  • 8