Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-05-2013, 01:44 AM   PM User | #1
ptcruiser
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
ptcruiser is an unknown quantity at this point
Using Javascript to change the value of field in form with a list of items

I have the following form that produces a list of about 25 pairs of values.
Code:
<?php
$records = $record->getRelatedSet("Value_List");

?>


<form data-ajax="false" action="none.php" method="post" name="values" id="values">


<?php $i=1;?>


<table border="1">
	<tr>
		<th>ValueA</th>
		<th>ValueB</th>
	</tr>



<?php foreach ($records as $related_record) 
{ ?>
	<tr>
		<td> 
        	<input   name="ValueA.<?php echo $i;?>" id="ValueA.<?php echo $i;?>" value=""/>     
	
 		</td>
	
		<td> 
    		<input   name="ValueB.<?php echo $i;?>" id="ValueB.<?php echo $i;?>" value=""/>     
	
 		</td>	
    
	</tr>

<?php $i=$i+1;
} ?>
<tr>	
<td><input type="submit" value="submit"/>	</td>
</tr>
</table>
	
	
	

		</form>
I would like to use javascript to change the value of ValueB based on the value of ValueA. I know how to do this if the form has one set of values but I don't know how to do it if you have an unknown number of value pairs in the list.

Thanks for your consideration.

Paul
ptcruiser is offline   Reply With Quote
Old 02-05-2013, 02:16 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Look at the page as HTML, not as PHP, for starters.

Bring up the page in your browser, select the VIEW menu, select SOURCE or PAGE SOURCE. Now see the HTML as the browser sees it.

Assuming you see something like this:
Code:
<table border="1">
	<tr>
		<th>ValueA</th>
		<th>ValueB</th>
	</tr>
        <tr>
		<td><input   name="ValueA.1>" id="ValueA.1>" value=""/></td>
		<td><input   name="ValueB.1>" id="ValueB.1>" value=""/></td>
        </tr>
        <tr>
		<td><input   name="ValueA.2>" id="ValueA.2>" value=""/></td>
		<td><input   name="ValueB.2>" id="ValueB.2>" value=""/></td>
        </tr>
        ... etc ...
</table>
Then I would first suggest:
(a) There is NO REASON to give both an ID and NAME to an <form> field.
Since you need the NAME to submit the <form> to PHP, drop the ID.
(b) It's a good idea to replace the period in those names with an underline.
Not terribly important, but in some cases it can simplify the JS code.

SO... Let's change all that to:
Code:
        <tr>
		<td><input   name="ValueA_1>" value=""/></td>
		<td><input   name="ValueB_1>" value=""/></td>
        </tr>
        <tr>
		<td><input   name="ValueA_2>" value=""/></td>
		<td><input   name="ValueB_2>" value=""/></td>
        </tr>
        ... etc ...
And then we can add JS code to easily work with those.

The best place to put the JS is *JUST BEFORE* the </body> tag, *not* in the <head>.

If you do so, you could have code such as this:
Code:
<script type="text/javascript">
(
  function( ) /* master anonymous function */
  {
      var form = document.getElementById("values");
      var inputs = form.getElementsByTagName("input");
      for( var i = 0; i < inputs.length; ++i )
      {
          var inp = inputs[i];
          if ( inp.name.substring(0,7) == "ValueA_" ) 
          {
              inp.onchange = valueAchange;
          }
      }

      function valueAchange( )
      {
          var fieldAName = this.name;  // get name of changed ValueA_xxx field
          var valA = this.value; // get the value of the given valueA_xx field 
          var fieldBName = fieldAName.replace("ValueA", "ValueB" ); // corresponding name!!  
          var valB = ... you supply the code to determine ValueB based on ValueA
          form[fieldBName].value = valB;
      }
  } // end of master anonymous function
)(); // self-invoke the master function
</script>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
ptcruiser (02-05-2013)
Old 02-05-2013, 01:23 PM   PM User | #3
ptcruiser
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
ptcruiser is an unknown quantity at this point
That is perfect. Thank you.
ptcruiser is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:22 AM.


Advertisement
Log in to turn off these ads.