/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] show hide div depending upon textfield value

I have included the form code I have been working on below: to illustrate the values that will appear in the form please use the following values:

A = 1000
B = 500
c = 3000

(A + ? / C = D

This is what I am trying to achieve:

If D >= 0.85 show form1 div

Else show form2 div

I was wondering if anybody can help me solve this or point me in the right direction as to how write the correct function to hide / show divs depending upon the value of a text field. Is this possible? Many thanks for your time.

bmc

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
<script language=”JavaScript” type=”text/JavaScript”>
function calculate() {

var valuea = parseInt(document.forms[‘form1’].elements[‘valuea’].value, 10);
var valueb = parseInt(document.forms[‘form1’].elements[‘valueb’].value, 10);
var valuec = parseInt(document.forms[‘form1’].elements[‘valuec’].value, 10);

var frm = document.forms[‘form1’].elements;

if (isNaN(valuea)) valuea = 0;
if (isNaN(valueb)) valueb = 0;
if (isNaN(valuec)) valuec = 0;

if (!isNaN(valuea) && !isNaN(valueb)) {
var tqVal = valuea + valueb;
frm[‘output’].value = tqVal;
var total = tqVal / valuec;
frm[‘total’].value = total;
}

return;
}

<style type=”text/css”>
<!–
#form1{ display: none;}
#form2{ display: none;}
–>
</style>

</head>
<body>
<form name=”form1″ id=”form1″ method=”post” action=””>
<table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td><div align=”center”><u><strong>Applicant 1 </strong></u></div></td>
<td> </td>
</tr>
<tr>
<td width=”15%”><div align=”right”>Value A </div></td>
<td width=”85%”><input name=”valuea” type=”text” id=”valuea” /></td>
</tr>
<tr>
<td><div align=”right”>Value B </div></td>
<td valign=”top”><input name=”valueb” type=”text” id=”valueb” /></td>
</tr>
<tr>
<td><div align=”right”>Value C </div></td>
<td valign=”top”><input name=”valuec” type=”text” id=”valuec” /></td>
</tr>
<tr>
<td> </td>
<td valign=”top”> </td>
</tr>
<tr>
<td><div align=”right”>Total:</div></td>
<td valign=”top”><input name=”output” type=”text” id=”output” /></td>
</tr>
<tr>
<td><div align=”right”>Total:</div></td>
<td valign=”top”><input name=”total” type=”text” id=”total”/></td>
</tr>
<tr>
<td>
<input type=”button” onclick=”calculate();” name=”Submit” value=”Submit” />
</div></td>
<td valign=”top”> </td>
</tr>
</table>
<p>&nbsp;</p>
<p></p>
</form>
</body>
</html>

I have used the following function to show / hide divs depending upon a select option before but I am not sure how to use this to show / hide divs depending upon the value of a text field. Any help or advice would be greatly appreciated.

function addOptions(_obj) {
if (_
obj.value == ‘>=0.85’) {
document.getElementById(‘test2’).style.display = ‘block’;
}
else {
document.getElementById(‘test2’).style.display = ‘none’;
}
}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@cs3mwJun 05.2007 — I havent tested this so if it doesnt work get back to me! Sorry Im dead busy at the mo!

This function should do it:

[CODE]function calculate() {

var valA = Number(document.all.form1.valuea.Value);
var valB = Number(document.all.form1.valueb.Value);
var valC = Number(document.all.form1.valuec.Value);

var calculation_value = ((valA + valB) / valC);

if (calculation_value > 0.85) {

document.getElementById("div1").style.visibility = "visible";
document.getElementById("div2").style.visibility = "hidden"; // in case you want to recalculate it
}
else {
document.getElementById("div1").style.visibility = "hidden";
document.getElementById("div2").style.visibility = "visible"; // in case you want to recalculate it
}

}
[/CODE]

Hope it works!! maybe small syntax errors in there tho sorry as i say im quite busy at the mo!!
Copy linkTweet thisAlerts:
@bmc79authorJun 06.2007 — Hi cs3mw,

That worked great I just had to change a couple of minor things but now it works perfectly. Heres the code I changed.

function calculate() {

var valuea = parseInt(document.forms['form1'].elements['valuea'].value, 10);

var valueb = parseInt(document.forms['form1'].elements['valueb'].value, 10);

var valuec = parseInt(document.forms['form1'].elements['valuec'].value, 10);

var calculation_value = ((valuea + valueb) / valuec);

if (calculation_value > 0.85) {

document.getElementById("div1").style.visibility = "visible";

document.getElementById("div2").style.visibility = "hidden"; // in case you want to recalculate it

}

else {

document.getElementById("div1").style.visibility = "hidden";

document.getElementById("div2").style.visibility = "visible"; // in case you want to recalculate it

}

}

Thanks a lot for your help it is very much appreciated, bmc
×

Success!

Help @bmc79 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...