Focal Point
[SOLVED] javascript for a textbox

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/5757088236

August 17, 2013, 08:40 AM
Jay Potter
[SOLVED] javascript for a textbox
I am trying to use javascript in a textbox, so that when a user hits enter on leaves the textbox, it fires off the code for a button click. I have many buttons on a page. When a user clicks on a button the color changes and clears out the color for all of the other buttons. That is how I identify the active button to execute.

First question, what event fires off when pressing enter? I believe it is onkeypress('enter') or something like that.

Secong question, I believe I fies off the button onclick event, but what do I put in for the ctrl? (onclick(ctrl))

var b1 = document.getElementById('button1');
var b2 = document.getElementById('button2');
var b3 = document.getElementById('button3');
var b4 = document.getElementById('button4');
var b5 = document.getElementById('button5');
var b6 = document.getElementById('button6');
var b7 = document.getElementById('button7');
var b8 = document.getElementById('button8');
var b9 = document.getElementById('button9');
var b10 = document.getElementById('button10');
var b11 = document.getElementById('button11');
var b12 = document.getElementById('button12');
var b13 = document.getElementById('button13');
var b14 = document.getElementById('button14');
var b15 = document.getElementById('button15');

color = "#4f8df1"

switch (color)

{

case b8.style.backgroundColor:

b8.button8_onclick(ctrl)

break

default:

document.write("Does color ", color , "match " ,

b8.style.backgroundColor , "!")

}

This message has been edited. Last edited by: Jay Potter,


WebFocus 8.1.5
iSeries/Windows
DB2/SQL/Access
Dev Studio
App Studio
Maintain
ReportCaster
August 18, 2013, 05:47 AM
Twanette
Hi Jay,
I may be wrong - but in a "proper" textbox pressing Enter would create a new line.
You could however trap when they leave the textbox with an onblur e.g.
<textarea name="TEXT1" rows="7" cols="70" onBlur="convert()" >

We used this to convert Enters to spaces e.g.
function convert() {
text = document.FORMNAME.TEXT1.value;
text = replace(text,unescape('%0D'),' ');
text = replace(text,unescape('%0A'),'');
document.FORMNAME.TEXT1.value = text;
}
function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length; 
    if ((strLength == 0) || (txtLength == 0)) return string;
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;
    var newstr = string.substring(0,i) + by;
    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);
    return newstr;
}
  


Perhaps this gives you some ideas for the first part of your question.


WebFOCUS 8.2.06 mostly Windows Server
August 19, 2013, 05:07 AM
Wep5622
quote:
First question, what event fires off when pressing enter? I believe it is onkeypress('enter') or something like that.


Not quite, no. The function in the onkeypress handler gets called with an event-data parameter that contains what key got pressed (among other things).

More details can be found in the reference: https://developer.mozilla.org/...Web/Reference/Events


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
August 19, 2013, 08:32 AM
J
You can just chain the text box through the gui to the buttons or whatever you want to show or hide and control that functionality from the parameters page.

The only issue is pressing enter won't unfocus in IE 9. The user will either have to click out or press tab. I don't know about IE10.


WebFOCUS 7.7.03/8.0.08
Dev Studio 7.7.03/8.0.08
App Studio 8.0.08
Windows 7
ALL Outputs
August 20, 2013, 11:47 AM
Jay Potter
I have tried the onblur event and have decided I do not want to use that event because it is a lostfocus type ebent and when clicking on a different button it fires off theoriginally selected button first.

I am still trying to identify when the enter button is selected. 'enter' is not working. Do I try and do an IF statement compareing against a 'ctrl' value? Does the onselect methoed fire off when the enter button is pressed?


WebFocus 8.1.5
iSeries/Windows
DB2/SQL/Access
Dev Studio
App Studio
Maintain
ReportCaster
August 21, 2013, 04:07 AM
Wep5622
In a textarea, the only events that get fired on pressing the enter key (or any other key) are onkeydown, onkeyup and (the combination of those two) onkeypress.

The first argument to the callback is usually the event object, as in the reference I linked. I don't think IBI is actively intercepting that call to replace it with their own callback, but they do omit passing the event object on to the callback function.

Try chainging the calls into these:
 onkeypress="textarea1_onkeypress(event, this)"

function textarea1_onkeypress(ev, ctrl) {



Here's an example that prints the keyboard character code after the key-press in the textarea (assuming yours has id=textarea1):
function textarea1_onkeypress(ev, ctrl) {
	var dbg = document.getElementById('textarea1');

	dbg.value += "["+ ev.keyCode + "]";
}



WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
August 21, 2013, 07:08 AM
Jay Potter
I thank you for all of the replies. I yhink I have found out what I am looking for. I failed to say the the textbox (not textarea) is part of a form. What I will be using is the onsubmit of the form, which fires when the user hits enter.


WebFocus 8.1.5
iSeries/Windows
DB2/SQL/Access
Dev Studio
App Studio
Maintain
ReportCaster