ellescuba27
Member
- Messages
- 273
- Reaction score
- 3
- Points
- 18
Here is a script that can detect when a user types text into a textbox. Unlike onChange, this fires immediatley after a keystroke. It ended up being a lot more advanced than I thought it would be, but nevertheless, here it is! You must put this at the END of your page, wrapped in <script> tags with the LANGUAGE attribute set to "Javascript". It uses mutation events so it might not work on older browsers.
To use it, just find where it says
and
and follow the instructions.
Feel free to use it for whatever purpose you want! If you edit it though, you don't have to, but why don't you post it back here for all to see?
Code:
var targ = null;
var targnum = 0;
var type = 0;
var whichone = null;
var getelementsbytagnametextboxes = document.getElementsByTagName("input");
function focusdetect(e) {
if (!e) {
e = window.event;
}
if (e.target) {
targ = e.target;
} else {
if (e.srcElement) {
targ = e.srcElement;
} else {
if (targ.nodeType == 3) {
targ = targ.parentNode;
}
}
}
getelementsbytagnametextboxes = document.getElementsByTagName("input");
for (i=0;i<=getelementsbytagnametextboxes.length-1;i++) {
if (getelementsbytagnametextboxes[i].type == "text") {
if (getelementsbytagnametextboxes[i] == targ) {
targnum = i;
type = 0;
}
}
}
getelementsbytagnametextboxes = document.getElementsByTagName("textarea");
for (i=0;i<=getelementsbytagnametextboxes.length-1;i++) {
if (getelementsbytagnametextboxes[i] == targ) {
targnum = i;
type = 1;
}
}
whichone = targnum;
}
function blurdetect() {
whichone = null;
}
function DOMchangedetect() {
getelementsbytagnametextboxes = document.getElementsByTagName("input");
for (i=0;i<=getelementsbytagnametextboxes.length-1;i++) {
if (getelementsbytagnametextboxes[i].type == "text") {
getelementsbytagnametextboxes[i].onfocus = focusdetect;
getelementsbytagnametextboxes[i].onblur = blurdetect;
}
}
getelementsbytagnametextboxes = document.getElementsByTagName("textarea");
for (i=0;i<=getelementsbytagnametextboxes.length-1;i++) {
getelementsbytagnametextboxes[i].onfocus = focusdetect;
getelementsbytagnametextboxes[i].onblur = blurdetect;
}
}
DOMchangedetect();
document.addEventListener("DOMNodeInserted", DOMchangedetect, false);
function typechangedetect() {
if (whichone != null) {
window.alert("You typed in textbox " + whichone + " type " + type);
// replace this alert with the code you want to do when the user types into the textbox. Note that the whichone variable contains which textbox the user typed into and type contains 0 or 1... input type="text" or textarea.
}
}
function otherchangedetect(e) {
if (!e) {
e = window.event;
}
if (e.target) {
targ = e.target;
} else {
if (e.srcElement) {
targ = e.srcElement;
} else {
if (targ.nodeType == 3) {
targ = targ.parentNode;
}
}
}
getelementsbytagnametextboxes = document.getElementsByTagName("input");
for (i=0;i<=getelementsbytagnametextboxes.length-1;i++) {
if (getelementsbytagnametextboxes[i].type == "text") {
if (getelementsbytagnametextboxes[i] == targ) {
targnum = i;
type = 0;
}
}
}
getelementsbytagnametextboxes = document.getElementsByTagName("textarea");
for (i=0;i<=getelementsbytagnametextboxes.length-1;i++) {
if (getelementsbytagnametextboxes[i] == targ) {
targnum = i;
type = 1;
}
}
window.alert("You typed in textbox " + targnum + " type " + type);
// replace this alert with the code you want to do when the user types into the textbox. Note that the targnum variable contains which textbox the user typed into and type contains 0 or 1... input type="text" or textarea.
}
document.onkeyup = typechangedetect;
document.oncut = otherchangedetect;
document.onpaste = otherchangedetect;
To use it, just find where it says
Code:
// replace this alert with the code you want to do when the user types into the textbox. Note that the whichone variable contains which textbox the user typed into and type contains 0 or 1... input type="text" or textarea.
Code:
// replace this alert with the code you want to do when the user types into the textbox. Note that the targnum variable contains which textbox the user typed into and type contains 0 or 1... input type="text" or textarea.
Feel free to use it for whatever purpose you want! If you edit it though, you don't have to, but why don't you post it back here for all to see?
Last edited: