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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie ?: Setting second value based on first value 1

Status
Not open for further replies.

jiggyg

Programmer
Oct 1, 2007
61
US
Hello List!

I'm new to JavaScript and am hoping someone out there can help me with an issue I'm having setting a value on a form...

I have two fields, 'Current Capacity' and 'Support Capacity' -> If the user inputs a value into the 'Current Capacity' field, I want to set the value of 'Support Capacity' to zero; and vice versa, if the user inputs a value into 'Support Capacity', I want to set the value of 'Current Capacity' to zero.

Here's the code I've got to try and do this:
Code:
if (document.forms[0].currentCapacity.value != "" ){
   (document.forms[0].supportCapacity.value) = 0;
   return;
}
else if (document.forms[0].supportCapacity.value != "" ){
   (document.forms[0].currentCapacity.value) = 0;
   return;
}

Problem is, when I click the 'Submit' button, nothing is happening; if I comment these two statements out, it works -- so I know my problem lies in these two statements...

Any help anyone could provide on how to correct this would be greately appreciated!

Thanks in advance for your time and help!
-jiggyg
 
[1] 'Current Capacity' is not CurrentCapacity; 'Support Capacity' is not SupportCapacity. If both have a space, replace the corresponding particles by elements["Current Capacity"] and elements["Support Capacity"]. [2] Apart from that, if it still errors, show the context where those lines appear. [3] There are circular constraints that do not look right. The first checking win and that breaks the symmetry. [4] If changed to "0", it means no "". The logic breaks open. Even if [1] rectified the problem, it is still not right.
 
Code:
if (document.forms[0].currentCapacity.value != "" ){
   document.forms[0].supportCapacity.value = 0;
}
else if (document.forms[0].supportCapacity.value != "" ){
   document.forms[0].currentCapacity.value = 0;

}

I don't think you need the return, and your code looks fine.
Are you submitting via javascript or are you calling this function onsubmit?

If your calling the function on submit you might want to change the way the function is called maybe attach the function to an onblur event on your textinput.
 
Thanks for the reply.

Yes, I'm aware that 'Current Capacity' and currentCapacity are not the same; I just typed it that way thinking it would be easier to read... the syntax in my statement is correct!

And, as I stated, my code is not working as written; when I click the submit button, it does nothing.

The user will see one of two fields, either a currentCapacity field if the space is a work station OR a supportCapacity field if the space is a support-type station -- both fields will NOT be displayed at the same time. So, if currentCapacity is shown (note: a value is required for that field!) &, hence, not empty ( ! ""), I want to set the supportCapacity field in the db to zero for that record.

-jiggyg
 
Thanks j4606! I removed the return, and it's working now!

Your help and knowledge is greatly appreciated!
-jiggyg
 
then your calling your function on submit. I can't exactly visualize what your doing but tsuji is probobly correct(as ussual) that your logic might break. Try putting in "returning true" as i think it serves the purpose or splitting up your logic on a input text basis and calling your code on blur.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top