Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird behaviours of Javascript in changing CSS on value

Status
Not open for further replies.

SBonfiglio

IS-IT--Management
Sep 15, 2008
24
IT
A very weird things are happening to me.
1. If I change the class of an Input Text field the value of the field is put at 0. So it seems that I have to save the value before changing the class, then I have to restore it.
2. It seems that it is impossible to change the CSS class again back to "class0" if the value goes back to zero again. The class remains "nonzerovalue"
This is the code.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
.zerovalue {
font-family: Arial, Helvetica, sans-serif;
font-size: 12pt;
font-weight: bold;
color: #666;
text-decoration: none;
background-color: #CCC;
text-align: right;
}
.nonzerovalue {
font-family: Arial, Helvetica, sans-serif;
font-size: 12pt;
font-weight: bold;
color: #000;
text-decoration: none;
background-color: #FFC;
text-align: right;
}
.smallbutton {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
font-weight: bold;
color: #000;
background-color: #DDD;
text-align: center;
vertical-align: middle;
height: 15px;
width: 15px;
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #EEE;
border-right-color: #666;
border-bottom-color: #666;
border-left-color: #EEE;
}
-->
</style>
<script language="javascript">
<!--
function modify(textinput,action){
var obj=document.getElementById(textinput);
var v = obj.value;
if(action=="add"){
obj.value++;
if(obj.value>99){
obj.value=99;
}
} else {
obj.value--;
if(obj.value<0){
obj.value=0;
}
}
v = obj.value;
if(obj.value=0) {
obj.className = 'zerovalue';
} else {
obj.className = 'nonzerovalue';
}
obj.value = v;
}
//-->
</script>
</head>
<body>
<h3>TEST OF CHANGING CSS STYLE ON VALUE </h3>
<form id="form1" name="form1" method="post" action="">
<h4>DESCRIPTION&nbsp;&nbsp;
<input name="L00100" id="L00100" type="text" class="zerovalue" value="0" size="2" maxlength="2" />
<input name="B00100sub" id="B00100sub" type="button" class="smallbutton" onClick="modify('L00100','sub');" value="-" />
<input name="B00100add" id="B00100add" type="button" class="smallbutton" onClick="modify('L00100','add');" value="+" />
</h4>
</form>
</body>
</html>
 
The problem is scope


This
JavaScript:
v = obj.value;
inside a function refers to a global variable.

This
JavaScript:
var v = obj.value;
inside a function creates a local variable.



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
a single '=' is an assignment operator. you are trying to compare two values so you need to use a '==' or '===' depending on what you are want.

so your code will work if you just change this line as shown

Code:
if(obj.value =[red]=[/red] 0) {

your code could use some improvement elsewhere but the above will, at least, let it work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top