Avatar of highlawn
highlawn
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Javascript function to change form fields

Hi - I wonder if anyone can help with what is hopefully a simple javascript request - if such a thing exists!

What I want to do is to examine a pair of fields - lets call them F1 and F2 - both of which are select boxes and both of which can have the values of "Yes" and "No".

If F1 is set to "Yes" then F2 must be set to "No" and vice versa. Now, I could probably do this using the form name and form field names and so on via the onchange event, but it struck me that there must be some way of writing a generic script that would allow you to pass the relevant parameters and cause the setting of the companion field to be the opposite.

So, the script being called doesn't know the form name (or ID) and doesn't know the field names/ids and doesn't know the value pairs - these all being passed at onchange time.

Does anyone know how I pass the fields to the JS function? For example if I have:

<form name="form1">
  <select name="f1" OnChange="change_values(this)">
      <option value="Yes">Yes</option>
      <option value="No">No</option>
  </select>
  <select name="f2" OnChange="change_values(this)">
      <option value="Yes">Yes</option>
      <option value="No">No</option>
  </select>

Open in new window


And the javascript...

 <script type="text/javascript">
  function change_values(form_field)
  {
  if (formfield.form.f1.value == "Yes"){
  formfield.form.f2.value = "No"
  }
else{
  formfield.form.f2.value = "Yes"
  }
  }
  </script>

Open in new window


But I don't want to hard code F1 or F2 or "Yes" or "No" and my JS is pretty basic!

Many thanks
Scripting Languages

Avatar of undefined
Last Comment
highlawn

8/22/2022 - Mon
Sudhindra A N

if you don't want to hard code 'F1' and 'F2',  still you can pass references to the javascript function. In this case you need to use some server side coding (languages like ColdFusion, PHP, asp etc)  for dynamic name.

but if it is a static html page then you have to hard code.
highlawn

ASKER
Sorry, I should have said that. I am coding everything using PHP - with which I am very familiar - and I want to generate the requisite javascript call within the PHP.

I would rather expect though, that the javascript function could be written once and included in the <head> of the page and then called via the PHP code as you are creating the <select> statements.
ASKER CERTIFIED SOLUTION
Michel Plungjan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
highlawn

ASKER
OK - that looks good. I fully understand the html - so, no issues there.

I think I understand the function - except I am not familiar with the selectedIndex property. Can you expand on what this is and when it can be used? Many Thanks.

Also, why do we need the window.onload piece? What if this form isn't loaded?

Cheers
Your help has saved me hundreds of hours of internet surfing.
fblack61
Michel Plungjan

I am using a trick since the selected index (index of the selected value in the drop down) is either 0 (yes) or 1 (no) in your case and !1 is 0 (NOT 1)  and !0 is 1so I negate the othe select on change
The on load is to initialise this situation so if select1 is yes, the script set select2 to no
highlawn

ASKER
OK - many thanks. Just what I was looking for. Perhaps you could indulge me just a touch further.

I'm assuming that "this" refers to the current field on the current form in this particular instance - i.e. the object with focus? If that is the case, then how does "this.form.field2" refer to the other field?

I would have thought that would be the equivalent of writing this.form.field1.form.field2 - if you see what I mean. Or does the "this" refer to a higher object?

Thanks
Michel Plungjan

this is the select this.form is the select's parent form and this.form.field2 is the sibling field where field2 is the actual field name but this.form are generic abstract object identifiers hence the php only tells the field name
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
highlawn

ASKER
OK - I think I get that. Many thanks for your help. I'll award the points.
highlawn

ASKER
Neat and simple and what I was after