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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Click return textbox value disappears (KeyCode 13)

Status
Not open for further replies.

stewartwebb

Programmer
Jan 6, 2004
92
GB
Hi all,

I have a strange problem that I can't figure out.
I've opened a pop-up window which has 1 textbox:-

Code:
<input name="hPART_QTY" type="text" id="hPART_QTY" tabindex="01"  onKeyUp="checkcr();" size="04" maxlength="04">

onKeyUp some javascript is called

Code:
function addParent()
{
    window.opener.form1.entpoint.value = "AddConsumable";
    window.opener.form1.hPART_QTY.value = document.form1.hPART_QTY.value
    window.opener.form1.submit();
}

function checkcr()
{
if(window.event.keyCode == 13)
    {
    addParent(); 
    window.close();
    }
}

However if I enter a number in the textbox then click return, everytime I click return the hPART_QTY.value disappears and becomes spaces.
I've put alerts in checkcr() as the first thing it does and the value is spaces before it gets to the script.

Any ideas how to solve this?

Thanks

Stewart.
 
Not sure what's going on for you. I changed a few things and this works for me in FF2 and IE7:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function checkcr(e, obj) {
   e = (e) ? e : event;
   var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
   if (keyCode == 13) {
      alert("return pressed\n\ntextbox value: " + obj.value);
   }
}

</script>
<style type="text/css"></style>
</head>
<body>

<input name="hPART_QTY" type="text" id="hPART_QTY" tabindex="01" onKeyUp="checkcr(event, this)" size="04" maxlength="04">

</body>
</html>

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Hi Kaht,

Thanks for your reply but I still have the same problem. I've post all the code, do you have any other ideas?

Code:
<HTML>
<HEAD>
<TITLE>Quantity (4 characters)</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="gcsglobal.css" type="text/css">
<script src="../gcs_scripts.js" language="Javascript" type="text/Javascript">

// nothing happens here... :D

</script>

<script language="javascript">
<!--
function addParent()
{
   window.opener.form1.entpoint.value = "AddConsumable";
            window.opener.form1.hPART_QTY.value = document.form1.hPART_QTY.value
    window.opener.form1.submit();
}

function checkcr(e, obj) {
   e = (e) ? e : event;
   var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
   if (keyCode == 13) {
              window.opener.form1.entpoint.value = "AddConsumable";
              window.opener.form1.hPART_QTY.value = document.form1.hPART_QTY.value
      window.opener.form1.submit(); 
      window.close();
   }
}


function setFocusQTY()
{
    document.form1.hPART_QTY.focus();
}

//-->
</script>

</HEAD>
<BODY onLoad="setFocusQTY(); whiteSpaceAll();">
<form  name="form1">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr align="center" valign="middle"> 
    <td width="25%" valign="top">&nbsp;</td>
    <td width="25%" valign="top">&nbsp;</td>
    <td width="25%" valign="top">&nbsp;</td>
    <td width="25%" valign="top">&nbsp;</td>
            </td>
  </tr>
  <tr align="center" valign="middle"> 
    <td colspan="4" valign="middle"><b>Enter Quantity</b></td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td valign="middle"> 
      <div align="right"></div>
    </td>
    <td valign="middle"></td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td align="center" valign="middle"> 
      <b>Quantity: </b></div>
    </td>
    <td valign="middle">
      <input name="hPART_QTY" type="text" id="hPART_QTY" tabindex="01" onBlur="removeNonNumerics(document.form1.hPART_QTY);" onKeyUp="checkcr(event, this)" size="04" maxlength="04">
    </td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td valign="middle"> 
      <div align="right"></div>
    </td>
    <td valign="middle"></td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td align="center" valign="middle"> </div>
    </td>
    <td valign="middle">
    </td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td valign="middle"> 
      <div align="right"></div>
    </td>
    <td valign="middle"></td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td valign="middle"> 
      <div align="right"></div>
    </td>
    <td valign="middle">
     <input type="button" value="Update" class="smallButton" onClick="addParent(); window.close();">
    </td>
    <td>&nbsp;</td>
  </tr>
  <tr>    
  </tr>
  <tr> 
    <td colspan="4" valign="top">&nbsp;</td>
  </tr>
</table>
</form>
</BODY>
</HTML>

The function whiteSpaceAll() with is called onLoad, is as follows:-

Code:
function whiteSpaceAll()
{
    for (i=0; i<document.form1.elements.length; i++)
      {
      if (document.form1.elements[i].type == "text")
        {
          inputWSpace(document.form1.elements[i]);
        }
      } 
}

function inputWSpace(myInput) 
{
    removeEscapeChars(myInput)
    newStr = " ";
	do
	{
        newStr = myInput.value;
        myInput.value=myInput.value.replace(/\s\s/, " ").replace(/^\s*/, "").replace(/\s*$/, "");
    } 
while(newStr != myInput.value)
}

function removeEscapeChars(Str)
{
    newStr = " ";
	do
	{
         newStr = Str.value;
         Str.value = Str.value.replace(/\"/, " ").replace(/\'/, " ");
    } 
while(newStr != Str.value)
}

And finally, the removeNonNumerics() function which is called onBlur of the textbox, is:-

Code:
function removeNonNumerics(myInput)
{
	myInput.value = myInput.value.replace(/\D*/g, "")
}

Thanks

Stewart
 
Hi,

On this subject, I added another text box on the page and the return works, then if I take the 2nd text box off it doesn't work.

Any thoughts?

Stewart.
 
Ahhhhhh.......

I ran into this problem a long time back. The page would always submit, but the submit button would not register as the element taking the submit action on the form - instead it was the textbox. I can't find the link anymore, but basically it works like this - if you have only 1 textbox on the page and you press enter on the textbox, it acts as a submit button. Even if you have 0 submit buttons on the page. If you have more than 1 textbox on the page and press enter, then the submit button is invoked and it is the element that triggers the submit action. Thus, if you have 0 submit buttons on the page with more than 1 textbox, the form will not be submitted when you hit the enter key in the textbox.

The way to make this work for your page is to put in an extra textbox and use CSS to hide it. This will suffice (make sure it's in the form with the other textbox):
Code:
<input type="text" style="display:none" />

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top