Solved

onchange and onkeyup together.

Posted on 2003-10-27
13
3,529 Views
Last Modified: 2008-02-01
Hi,
I wrote a validation function,which makes for example 1234567  ---> 1,234,567

I use it in a textbox and I wanna also use onchange event for the same textbox. The function I wrote runs when unkeyUp activated.

However unkeyUp and onchange events doesn't work together?

Do u have any idea?
 
My function:
function Fcm2(thisis) {
    var tempstr,newstr,str,i,str2 ;
    var commapos,aftercomma,commacount;
    i=0;
    str = thisis.value;
        if (str.indexOf(".") < 0) {
        while ( 15 > i) { str = str.replace(",",""); i=i+1; }
    commacount=0;
    commapos=str.indexOf(";");

        if (commapos>=0) { aftercomma=str.substr(commapos); str=str.substr(0,commapos); }
        else aftercomma="";

        if (str.length > 3) {
           tempstr=str;
           newstr="";
           while (tempstr.length > 3)
           {  newstr=","+tempstr.substr(tempstr.length-3)+newstr;
              tempstr=tempstr.substr(0,tempstr.length-3);          }
        thisis.value = tempstr+newstr+aftercomma;
    }
        }
    return true;
}


---------------------
function calcul(thh){
alert("change");
}

function press(thisis){
if(window.event.keyCode < 47 && window.event.keyCode > 57) { window.event.keyCode = 9008999; }
if(window.event.keyCode == 9008999) {return false;  } else {Fcm(thisis) }
}

---------------------
<td width=80% height=20><input value="" class=numstd onchange="calcul(this)"  onkeyUp="press(this)" name=deneme size=40 maxlength=40></td>

0
Comment
Question by:edoyuk
13 Comments
 
LVL 5

Expert Comment

by:alambres
ID: 9625999
if you want to run calcul function just after press function you must call it in the onkeyup too. onchange event just triggers when you exit the field (if its value has changed, of course). there's no evaluation of value changes when pressing keys, just when exiting.

something like that:

function calcul(thh){
if (thh.value != thh.oldValue) alert("change");
thh.oldValue = thh.value;
}



<td width=80% height=20><input value="" class=numstd   onkeyUp="press(this);calcul(this);" name=deneme size=40 maxlength=40 oldValue=""></td>

hope it helps ya.

peace

alambres
0
 

Author Comment

by:edoyuk
ID: 9626135
No alambres with this method it goes to database every time a key is pressed!!!
This is not that I want to.
0
 
LVL 5

Expert Comment

by:alambres
ID: 9626308
what do you want then? I don't get your aim
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:edoyuk
ID: 9626333
With onkeyup I want to use my validate function but onchange doesnt work when I use it.
0
 
LVL 5

Expert Comment

by:alambres
ID: 9626406
this is then:
<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript">
function Fcm(thisis)
{
    var tempstr,newstr,str,i,str2 ;
    var commapos,aftercomma,commacount;
    i=0;
    str = thisis.value;
        if (str.indexOf(".") < 0) {
        while ( 15 > i) { str = str.replace(",",""); i=i+1; }
    commacount=0;
    commapos=str.indexOf(";");

        if (commapos>=0) { aftercomma=str.substr(commapos); str=str.substr(0,commapos); }
        else aftercomma="";

        if (str.length > 3) {
           tempstr=str;
           newstr="";
           while (tempstr.length > 3)
           {  newstr=","+tempstr.substr(tempstr.length-3)+newstr;
              tempstr=tempstr.substr(0,tempstr.length-3);          }
        thisis.value = tempstr+newstr+aftercomma;
    }
        }
    return true;
}

function calcul(thh){
if (thh.value != thh.oldValue) alert("change");
thh.oldValue = thh.value;
}

function press(thisis){
if(window.event.keyCode < 47 && window.event.keyCode > 57) { window.event.keyCode = 9008999; }
if(window.event.keyCode == 9008999) {return false;  } else {Fcm(thisis) }
}



</script>

</HEAD>
<BODY>

<input value="" class=numstd onblur="calcul(this)"  onkeyUp="press(this)" name=deneme size=40 maxlength=40>

</BODY>
</HTML>
0
 

Author Comment

by:edoyuk
ID: 9626408
Simply,
I want to use both validation and onchange event?

If you write 1234567 to the textbox it will make it 1,234,567 every time you write a number and then if you press tab button it will save it to the database?

Do you have a solution for that?

Thank you....
0
 

Author Comment

by:edoyuk
ID: 9626417
alambres,
It is nor efficient to use onBlur because I wanna do some savings when data is changed in the textbox...
 
0
 
LVL 5

Expert Comment

by:alambres
ID: 9626445
what do you understand by "data is changed"? I repeat: onchange event triggers when EXITING (=ONBLUR) the field. You should check if the value's changed when blur. Otherwise, you'll be doing the saving to DB every keyPress, which you said you don't want to. So?
0
 

Author Comment

by:edoyuk
ID: 9626501
BUT how can I check easily(without global variables) if the value's changed when blur?
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9626515
You kill the onChange event by changing the field value by previous events.
So you have to check for changes on your own.

Like this:

<input type=text value="" onFocus="this.oldValue=this.value" onBlur="if(this.value!=this.oldValue)alert('changed')">

Good luck,
NetGroove


0
 
LVL 5

Accepted Solution

by:
alambres earned 300 total points
ID: 9626560
man, you didn't test my solution, did ya? Again: just need to add a custom attribute to the field (see that NetGroove is suggesting the same), you can name it as you wish , I name it oldValue. Here you store the value to do the comparison when checking the changes in the ONBLUR event. please, READ the suggestions and TEST it. Here's the code, again:

<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript">
function Fcm(thisis)
{
    var tempstr,newstr,str,i,str2 ;
    var commapos,aftercomma,commacount;
    i=0;
    str = thisis.value;
        if (str.indexOf(".") < 0) {
        while ( 15 > i) { str = str.replace(",",""); i=i+1; }
    commacount=0;
    commapos=str.indexOf(";");

        if (commapos>=0) { aftercomma=str.substr(commapos); str=str.substr(0,commapos); }
        else aftercomma="";

        if (str.length > 3) {
           tempstr=str;
           newstr="";
           while (tempstr.length > 3)
           {  newstr=","+tempstr.substr(tempstr.length-3)+newstr;
              tempstr=tempstr.substr(0,tempstr.length-3);          }
        thisis.value = tempstr+newstr+aftercomma;
    }
        }
    return true;
}

function calcul(thh){
if (thh.value != thh.oldValue) alert("change");
thh.oldValue = thh.value;
}

function press(thisis){
if(window.event.keyCode < 47 && window.event.keyCode > 57) { window.event.keyCode = 9008999; }
if(window.event.keyCode == 9008999) {return false;  } else {Fcm(thisis) }
}



</script>

</HEAD>
<BODY>

<input value="" class=numstd onblur="calcul(this)"  onkeyUp="press(this)" name=deneme size=40 maxlength=40>

</BODY>
</HTML>
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10346042
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: alambres {http:#9626560}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript/JQuery - HtmlEncode (?) Ajax Result 1 67
Grunt script for Build Process 1 44
alert(innerHTML); 8 32
on window close, not beforeunload 6 20
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

821 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