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>
1

3 Answers 3

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;"
Sign up to request clarification or add additional context in comments.

3 Comments

getElementsByClassName Only works in IE9 and none of the previous IEs. quirksmode.org/dom/w3c_core.html#t11
@Trufa I use it to access class in iOS App .Thank you for your answer. ^^
getElementsByClassNamereturns a 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.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.