Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

auto populate the form field 1

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,

In simplest terms, I have a form with two text fields(txtField1, txtField2).

in the txtField1 the user enters the values in the XXX-YYYY format.

once the user leaves that txtField1, I want to populate the txtField2 with the last portion of the txtField1 value, i mean I want to populate txtField2 with YYYY.

Thanks

-DNG
 
Hi

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title></title>
<script type="text/javascript">
function pop(what)
{
  what.form.txtField2.value=what.value.replace(/.*-/,'')
}
</script>
</head>
<body>
<form action="#">
<p>
<input type="text" name="txtField1" onchange="pop(this)">
<input type="text" name="txtField2">
</p>
</form>
</body>
</html>

Feherke.
 
works fine.thanks.

One question though, the txtField1 is populated by auto suggest drop down values. I mean if I enter some characters, I get the values to choose from and then I can select the value.

So If I do that. My txtField2 is getting only the characters I type and it is not picking the value that I picked for the txtField1 from the autosuggest options.

not sure if i made it clear. Your functions seems like only using the characters entered in txtField1.

thanks

-DNG
 
Hi

Well, that script does exactly what you asked for :
DNG said:
once the user leaves that txtField1
Probably that auto-complete functionality populates the field without passing the focus to the field again. Even more, I have a feeling that there is no catchable event.

Sadly the [tt]watch[/tt] does not seem to work for visual objects. So the best alternative I see is to double it with a manual check.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title></title>
<script type="text/javascript">
function pop(what)
{
  what.form.txtField2.value=what.value.replace(/.*-/,'')
}
[red]var ori
var t1
function chk()
{
  if (ori!=t1.value) {
    ori=t1.value
    pop(t1)
  }
}
window.onload=function() {
  t1=document.getElementById('txtField1')
  ori=t1.value
  window.setInterval('chk()',1000)
}[/red]
</script>
</head>
<body>
<form action="#">
<p>
<input type="text" name="txtField1" id="txtField1" onchange="pop(this)">
<input type="text" name="txtField2" id="txtField2">
</p>
</form>
</body>
</html>

Feherke.
 
Thanks Feherke,

Sorry i didnt get back to this thread earlier. Got tied up with other things.

i will give you the code a shot and will let you know.

Have a star.

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top