View Full Version : Can we have controls dependant on each other?
vijk2001
08-12-2002, 05:53 PM
Hi ..
Say i have two text edit controls and want the display on one to reflect display on the other..
text edit 1.......enter some value A12345
text edit 2.......takes only A123(first four chars ) of text edit 1
So we have contols dependancy...
and how do i embed this java script in HTML??
regards a
..............
beetle
08-12-2002, 07:20 PM
How's this<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN" />
<HTML>
<HEAD>
<TITLE>Input Test</TITLE>
<script language="javascript" type="text/javascript">
function checkVal(i1, i2)
{
var obj1 = getObj(i1);
var obj2 = getObj(i2);
if (obj2.value != obj1.value.substring(0,4))
{
alert("Data incorrect");
obj2.select();
obj2.focus();
return false;
}
alert('Data Ok');
}
function getObj(id)
{
return (typeof document.layers != 'undefined') ? document.layers[id] : (document.getElementById || document.all)(id);
}
</script>
</HEAD>
<BODY>
<form>
<input type="text" id="input1" /><br>
<input type="text" id="input2" /><br>
<input type="button" name="checkit" value="Check It" onClick=";checkVal('input1','input2');" />
</form>
</BODY>
</HTML> Note: Thanks to jkd (http://www.codingforums.com/showthread.php?s=&threadid=3844) for the getObj() function
beetle, you used the function in the inappropriate context. getElementById only returns matching id's, not names. Which means that won't work, unless you change the <input/> names to ids. And that still won't work in NS4, due to using the layers array.
beetle
08-12-2002, 07:42 PM
You're right. Sorry, I knew that....did it in too much of a hurry....*edit edit edit* :D
I would think something like:
<input type="text" name="master" value="sometext"/>
<input type="text" name="slave" onkeydown="return (this.form.master.value.indexOf(this.value.substring(0,4)) == 0 && this.value.length <= 4)"/>
Or something close to that should do it.
beetle
08-12-2002, 08:10 PM
Ya, something like that could work, but I should note that onKeyDown won't cancel in IE4, and won't fire on a backspace keypress either.
Originally posted by beetle
and won't fire on a backspace keypress either.
I don't think part matters. Just saves the browser from having to execute a line of JS everytime backspace is hit.
adios
08-13-2002, 12:50 AM
As a function:
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
function limitChars(limit, fromField, toField) {
toField.value = (fromField.value.length<limit) ?
fromField.value : fromField.value.substring(0,limit);
}
</script>
</head>
<body>
<form>
text edit 1: <input type="text" name="text_edit_1"
onkeyup="limitChars(4,this,text_edit_2)">
text edit 2: <input type="text" name="text_edit_2" readonly="readonly"
onfocus="if(!this.readOnly)this.blur()">
</form>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.