Connecting Tech Pros Worldwide Forums | Help | Site Map

Cold Fusion - onChange problem

Newbie
 
Join Date: Feb 2007
Posts: 4
#1: Feb 22 '07
Hello,

I have a ColdFusion online application that has a page having 2 textboxes.
Corresponding to these 2 textboxes I have a Custom tag in coldfusion where the textbox is defined by <input type=text .....>. In the onChange event a coldfusion attribute is called (like, onChange="#attributes.onChange#").

Now in the cold fusion page the 2 textboxes are defined using the single Custom tag with different name (say m and x). On the onChange event 2 javascript functions are called which does some validations. On the OnChange event of m javascript function mChange() is called and on the OnChange event of x javascript function xChange() is called.

The functionality is
1) whenever I enter anything in m, the x should get populated from database call and vice versa. Also, I can enter a value of x in m. Then the value will get switched, i.e. the value of x entered in m will go to x textbox and value of m retrieved from database will got to m textbox. This switching is done in javascript functions stated earlier, using m.value=M1 and x.value=X1;
This can occur vise versa.
2) Whnever I delete text in anyone of m or x both of the text box will get deleted.
These are working fine.
But, when I am entering a value of x in m,
m.value is now 'X1'
x.value is now ""
After database call and javascript function,
m.value becomes "M1" as retrieved from database and changed by Javascript.
x.value becomes "X1" as switched by Javascript.

Now without changing the value of m textbox when I tab out from the m textbox the onChange() again gets fired.
Please lend me a solution to this. As far as I know, the onChange now shouldn't get fired, since now m.value = M1 and I haven't changed the value.

Thanks,
Subho

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Feb 22 '07

re: Cold Fusion - onChange problem


Possibly the onChange gets fired because the value has been altered by the Javascript. The onchange most likely fires because when you lost focus the value is different from when you gained focus. One possible workaround is to give focus to another element after/before the change. If that doesn't work, post your code.
Newbie
 
Join Date: Feb 2007
Posts: 4
#3: Feb 22 '07

re: Cold Fusion - onChange problem


Quote:

Originally Posted by acoder

Possibly the onChange gets fired because the value has been altered by the Javascript. The onchange most likely fires because when you lost focus the value is different from when you gained focus. One possible workaround is to give focus to another element after/before the change. If that doesn't work, post your code.

Hello,

Thanks for the solution, but it is not working that way..Here is the code.

Custom tag is defined as:
[HTML]<input type="text" name="#attributes.name#"
size="13" maxlength="13" onChange="#attributes.onChange#
value="#attributes.value#">
[/HTML]
The Instance of Custom tag is created as:
[HTML]<CF_mytag
name = "m"
value = ""
onChange = "mChanged();"
>

<CF_mytag
name = "x"
value = ""
onChange = "xChanged();"
>[/HTML]

Let say I have entered X1 value in texbox m. Then m.value=X1 and database query will fetch a corresponding M1 for the X1 value which should go in texbox m and the value of texbox m is switched to texbox x.

The Javascript functions are identical,
Expand|Select|Wrap|Line Numbers
  1. function mChanged() 
  2. {    
  3. with (document.all)    
  4. {
  5.   l_m_text = m.value;    
  6.   if (sComponent.m != "")   //This is received from Database
  7.     m.value = sComponent.m;
  8.   else if (l_m_text != "")
  9.     alert("Invalid");
  10.   x.value = sComponent.x;
  11. }
  12. }
  13.  
  14. function xChanged() 
  15. {    
  16. with (document.all)    
  17. {
  18.   l_x_text = x.value;
  19.   if (sComponent.m != "")   //This is received from Database
  20.     x.value = sComponent.x;
  21.   else if (l_x_text != "")
  22.     alert("Invalid");
  23.   m.value = sComponent.m;
  24. }
  25. }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Feb 23 '07

re: Cold Fusion - onChange problem


document.all is IE-specific (and supported by Opera, I think). Use the standard document.getElementById instead.

In your mChanged/xChanged functions, after the last line, send focus to some other field. Try this, e.g.
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('someotherelement').focus();
Does that solve the problem?

Is the custom tag just what you have shown? If it is, there's no need for it - it's just simple HTML.
Newbie
 
Join Date: Feb 2007
Posts: 4
#5: Feb 26 '07

re: Cold Fusion - onChange problem


Quote:

Originally Posted by acoder

document.all is IE-specific (and supported by Opera, I think). Use the standard document.getElementById instead.

In your mChanged/xChanged functions, after the last line, send focus to some other field. Try this, e.g.

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('someotherelement').focus();
Does that solve the problem?

Is the custom tag just what you have shown? If it is, there's no need for it - it's just simple HTML.

Hello,

Thanks for the solution but the assigning of focus to some other control is not working. Also the custom tag has some other functionality which details that whenever something is entered and tabbed out a modal window will open which will actually call a cfquery in another .cfm file which fetches the database records.

Thanks,

Subho
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Feb 26 '07

re: Cold Fusion - onChange problem


Perhaps you could post the code for the custom tag.
Reply