?
Solved

change value of date inputs if the main date has changed

Posted on 2013-07-01
1
Medium Priority
?
301 Views
Last Modified: 2013-07-15
I have a form that will hold expense receipts.  I have a dynamic table which allows for users to enter a receipt, and add a new row for each item on that receipt.

If they initially choose July 1 for the first table row, then create three more rows after that, and go back and adjust the date to July 2, I want the newly created rows to refresh to the new date.

Right now, I have the date field cloning for each table row that is added, but cannot figure out how to change all dates if the main row's date changes.

Also, I have each new row's date field set to read only, so users cannot select multiple dates with the assumption that all entries on one receipt will be for the same day.

			<div id="row">
				<div id="left">
					<strong>Receipt Items:</strong>
				</div>
				<div id="right">
					<i>Only add another line to the receipt below IF you have multiple items on one receipt.</i>
					<br /><br />
					<table border="0" width="825px" cellspacing="0" cellpadding="5" name="receipts" id = "receipts">
						<thead>
							<tr>
								<th class="colheader" width="125px">Receipt #</th>
								<th class="colheader" width="120px">Date</th>
								<th class="colheader" width="120px">Category</th>
								<th class="colheader" width="120px">Description</th>
								<th class="colheader" width="120px">Amount</th>
								<th class="colheader" width="145px"><span class="boldblacklinks"><a href="#" id="add">[Add +]</a></span></th>
							</tr>
						</thead>
							<tbody class="lineBdy">
								<tr id="line_1" class="spacer">
									<td><input type="text" class="receipt fieldclasssm" id="recLineReceipt[]" name="recLineReceipt[]" size="7" value = "<?=$receiptNumber?>"/></td>
									<td><input type="text" class="date fieldclasssm" id="recLineDate[]" name="recLineDate[]" size="10" value = "<?=date("m/d/Y", strtotime($today))?>"/></td>
									<td><select name="selectCategory[]" class="fieldclasssm">
												<option value = "">Select a Category...</option>
												<?php //Get Categories
												
												$getCats = mysql_query("SELECT id, nominalName FROM expense_nominalCodes ORDER BY id") or die("Get Cats: " . mysql_error());
												
												if(mysql_num_rows($getCats) > 0)
													{
													while($catData = mysql_fetch_array($getCats))
														{
														echo '<option value = "'.$catData['id'].'">'.$catData['nominalName'] . '</option>';
														}
													}
												?>
										</select>
									</td>
									<td><input type="text" class="lineDescr fieldclasssm" name="recLineDescr[]" id="recLineDescr[]" value = "<?=$_POST['recLineDescr']?>" size="40" /></td>
									<td colspan = "2"><input type="text" class="amt fieldclasssm" name="recLineAmount[]" id="recLineAmount[]" value = "<?=$_POST['recLineAmount']?>" size="12" /></td>
								</tr>
							</tbody>
						</table>
					</div>
			</div>
			<div align="center"><br /><br /><input type="submit" name = "saveAdd" class="btn" value = "Save & Add Another Receipt" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name = "saveAdd" class="btn" value = "Save as Draft" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" class="btn"name="saveDraft" value = "Save & Finalize Expense Report" /><br /><br /></div>
	</div>
</form>
		
<script type="text/javascript">

// Dollar Amounts
		$(function() {
			$('.amt').blur(function() {
				$(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 2, groupDigits: false });
			})
			.keyup(function(e) {
				var e = window.event || e;
				var keyUnicode = e.charCode || e.keyCode;
				if (e !== undefined) {
					switch (keyUnicode) {
						case 16: break; // Shift
						case 17: break; // Ctrl
						case 18: break; // Alt
						case 27: this.value = ''; break; // Esc: clear entry
						case 35: break; // End
						case 36: break; // Home
						case 37: break; // cursor left
						case 38: break; // cursor up
						case 39: break; // cursor right
						case 40: break; // cursor down
						case 78: break; // N (Opera 9.63+ maps the "." from the number key section to the "N" key too!) (See: http://unixpapa.com/js/key.html search for ". Del")
						case 110: break; // . number block (Opera 9.63+ maps the "." from the number block to the "N" key (78) !!!)
						case 190: break; // .
						default: $(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: -1, eventOnDecimalsEntered: true });
					}
				}
			});
		});
			
	//Add new table row & clone date field
	$('#add').on('click', function(){
	   addReceiptItem();
	   	$('.date').focus(function() {
			$(this).select();
		});
	   	$('.receipt').focus(function() {
			$(this).select();
		});	

	$('#receipts td:contains("recLineAmount")').addClass('amt');
	});
	
	function addReceiptItem(){
		 var lastID = $('tr[id*="line_"]').length,    
			 newTds = $('tr[id="line_' + lastID + '"] td').clone(),
			 newRow = document.createElement('tr');
	   
		// add new id and class to row, append cloned tds
		$(newRow)
			.attr('id', 'line_' + (lastID + 1 ))
			.attr('class', 'spacer')
			.append(newTds);
			
		//empty out the fields, except the one you want to keep populated
		// $(newRow).find('input').not(':eq(0)').val('');
		
		 // $(newRow).find('input').not(':eq(0)').val('');
		
		
		$(newRow).find('input').not(':eq(0)').not(':eq(0)').val('');
		$(newRow).find('class').not(':eq(0)').not(':eq(0)').val('');
		
		//add the new row to the table body
		$('tbody.lineBdy').append(newRow);
		$('.receipt').attr('readonly', true);
		$('.date').attr('readonly', true);
		};		
		


</script>	

Open in new window

0
Comment
Question by:t3chguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 2000 total points
ID: 39309669
your line at 117 is actually disabling all the date fields including the one in the main row, so not sure how the user will change it

However just add this line to your method addReceiptItem() at the end of it after line 117. instead of keyup, you could also use change the first will change all the fields as and when you type, the second method will updated the fields on focus out
$('.date').keyup(function (){
    $('.date').val($('.date').val());
});

Open in new window

0

Featured Post

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question