3 Replies - 11793 Views - Last Post: 21 April 2008 - 12:39 PM

#1 dedman  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 22-April 05

Calling same function from two places, who called?

Posted 21 April 2008 - 09:53 AM

I have the same function being called from two places. It checks that one date is larger than the other date so users can't enter a start date later than the end date or an end date earlier than the start date.

Is there a way to tell which element called the function? Or should I just add a parameter to the function that tells me who called?

Here are the callers (I know it says "readonly" and I am using the onchange event, there is a calendar for changing the date):
<tr>
   <td><span id="Green_Xsmall">Start Date: </span></td>
   <td><input type="text" id="a_date_c" name="startDate"
			size="12" readonly="readonly" 
			onchange="checkDateRange(startDate, endDate, 
			'Start Date: must be prior to the End Date:')" /></td>
</tr>
<tr>
   <td><span id="Green_Xsmall">End Date: </span></td>
   <td><input type="text" id="b_date_c" name="endDate"
			size="12" readonly="readonly" 
			onchange="checkDateRange(startDate, endDate, 
			'End Date: must be a date after the Start Date:')" /></td>
</tr>



And here is the script:
function checkDateRange(elementOne, elementTwo, string){
//date comparision of two form element inputs

	var dateStringOne = elementOne.value.split('-');
	dateStringOne[1] = dateStringOne[1] - 1;
	var dateObjectOne = new Date();
	dateObjectOne.setMonth(dateStringOne[1]);
	dateObjectOne.setDate(dateStringOne[2]);
	dateObjectOne.setFullYear(dateStringOne[0]);

	var dateStringTwo = elementTwo.value.split('-');
	dateStringTwo[1] = dateStringTwo[1] - 1;
	var dateObjectTwo = new Date();
	dateObjectTwo.setMonth(dateStringTwo[1]);
	dateObjectTwo.setDate(dateStringTwo[2]);
	dateObjectTwo.setFullYear(dateStringTwo[0]);
	if (dateObjectOne > dateObjectTwo){
		alert(string);
		elementTwo.setAttribute('value', '');
	}
}



The js code may not be very professional looking, but it works and I am just trying to modify what someone else (who no longer works here) created.

Is This A Good Question/Topic? 0
  • +

Replies To: Calling same function from two places, who called?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Calling same function from two places, who called?

Posted 21 April 2008 - 12:01 PM

You can pass a reference to the control to the function using the "this" keyword. So for instance if I had a button I could use....

<input type="button" name="mybutton" id="mybutton" value="click me" onclick="myfunction(this)"/>



This would pass the reference of the button to the myfunction() function. This is exactly like using document.getElementById("mybutton"); except use the button in passing a reference to itself. So in the function you can use the variable to then decide who called the function.

// Would essentially change the width of any object passed to it.
// In our case, the button we passed to it through the onclick handler.

function myfunction(obj) {
     obj.style.width = 100 + 'px';
}



I hope that is making sense. If you read the code in the onclick handler it makes sense... on click call my myfunction and give it "this" control.

Enjoy!

"At DIC we be button passing code ninjas... we also pass on things like 'the buck', and 'the ugly chicks'" :snap:

This post has been edited by Martyr2: 21 April 2008 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 dedman  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 22-April 05

Re: Calling same function from two places, who called?

Posted 21 April 2008 - 12:31 PM

View PostMartyr2, on 21 Apr, 2008 - 03:01 PM, said:

"At DIC we be button passing code ninjas... we also pass on things like 'the buck', and 'the ugly chicks'" :snap:

What about passing 'the gas'? Especially after this: Caffiene Lounge Link

Back to the issue at hand...Thank you for your help. I added 'this' to both function calls and added 'obj' parameter to the function. Then checked if (obj.name == 'startDate') in the function. It solved my issue. The original js code always cleared the value of the endDate input. I wanted to clear the value of the input field the user was changing.

Again, many thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Calling same function from two places, who called?

Posted 21 April 2008 - 12:39 PM

Sure sure, we pass on the gas too from time to time. It is what we do when we are not coding. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1