Forum Moderators: open

Message Too Old, No Replies

Javascript and CSS.shouldn't this work?

         

kiwi1066

12:25 pm on Sep 27, 2005 (gmt 0)

10+ Year Member




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.box { width : 2em;
height :2em;
}
</style>
<script language="JavaScript">
function change(){
document.getElementById("box").style.width = "0.7em";
document.getElementById("box").style.height = "0.7em";

}
</script>
</head>
<body>
<div id ="box" onMouseOver="change()"><input type="checkbox" name="checkbox" class = "box" value="checkbox"></div>
</body>
</html>

benihana

12:32 pm on Sep 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the initial style wont be applied as your css is referencing a class, but the html and JS need an ID

kiwi1066

1:36 pm on Sep 27, 2005 (gmt 0)

10+ Year Member



But when I view it through my browser the checkbox is 2ems and I thought that my id was "box"

benihana

1:42 pm on Sep 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



.box

in your css will affect anything that has: class="box"

try changing it #box

n.b. you havent said what problem your actually having, so ive no idea if this will help

Bernard Marx

4:41 pm on Sep 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a little confusing, kiwi.

The containing div: id="box"
The checkbox: class="box"

The stylesheet rule (.box) applies to the checkbox,
whilst the scripted style changes, in fn change, apply to the div element.

If you give the div a visible border, you'll see that that the dims of the div are affected when you mouseover. I guess that's not what you're after.

kiwi1066

8:27 pm on Sep 27, 2005 (gmt 0)

10+ Year Member



No it is not. What I am trying to do is have the checkboxes a certain size if javascript is disabled but if javascript is enabled the have the checkboxes size changed.
Sort of like

style -> size 1em

onload -> javascript function(change size to .6em)

I am thinking that the function needs to be along the like of


function change(){
document.getElementByTagName("input").style.width = "0.6em";
document.getElementByTagName("input").style.height = "0.6em";

}


and then have <body onload = "Change()">
and lose the div thing.
what do you think?

kiwi1066

9:26 pm on Sep 27, 2005 (gmt 0)

10+ Year Member



OK.. the following works..thanks!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.box { width : 1em;
height :1em;
}
</style>
<script language="JavaScript">
function changeCheckboxes() {

// find all input controls:
var els=document.getElementsByTagName("input");

// for each input element...
for(var i=0; i < els.length; i++) {

if (els[i].type=='checkbox') { // its a checkbox

// change size
els[i].style.width= ".6em";
els[i].style.height=".6em";

}

}
}
</script>
</head>
<body LANGUAGE=JavaScript onload ="changeCheckboxes()">

<table border="1">
<tr>
<td><input type="checkbox" name="checkbox" class = "box" value="1"></td>
<td><input type="checkbox" name="checkbox" class = "box" value="2"></td>
<td><input type="checkbox" name="checkbox" class = "box" value="3"></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox" class = "box" value="4"></td>
<td><input type="checkbox" name="checkbox" class = "box" value="5"></td>
<td><input type="checkbox" name="checkbox" class = "box" value="6"></td>
</tr>


</table>

</body>
</html>