473,738 Members | 7,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need Javascript OO help

I have the some HTML and JavaScript code that implements
a type-ahead capability for a text input control linked to a
select control (see end of message). The type-ahead
functionality works well. The only problem is that I'd like
to implement an external event sink so that when the text
control loses focus, I can do ... well whatever I might
happen to want the event sink to do.

The code below will even sink the onblur event of the text
control and will behave properly. (To see it in action,
change function for this.onNotInLis t to alert the user
when the text control loses focus.)

The problem is, I'd like to be able to set the notinlist sink
to a function outside the class. I'm not able to get the
syntax right for it. I've tried, for example,

var ta = new TypeAhead();
ta.onNotInList = pointerToMyNotI nListHandler;

but I can't seem to get it to work.

What am I missing? What do I need to do?

javacript dude
<html>
<head>
<script language="javas cript">
function TypeAhead(txtPa rent, selChild){
this.txtParent = txtParent;
this.selChild = selChild;
this.onNotInLis t = function(){void (0);}
//this.onNotInLis t = function(){aler t("not in list");}
var notInList = this.onNotInLis t;

this.txtParent. onkeyup = function(){
var optionText = txtParent.value .toUpperCase();
var options = selChild.option s;
for (var i = 0; i < options.length; i++){
if(optionText.l ength > 0 && options[i].text.toUpperCa se().indexOf(op tionText)==0){
options[i].selected=true;
return;
}
}
selChild.select edIndex = -1;

}

this.txtParent. onblur = function(){
if (selChild.selec tedIndex == -1) {
notInList();
}
}

this.selChild.o nclick = function(){
var selectedIndex = selChild.select edIndex;
if (selectedIndex > -1){
txtParent.value = selChild.option s[selectedIndex].text;
}
}

}

TypeAhead.proto type.contructor = TypeAhead;

var ta;

function NotInList(){
alert("not in list");
}

window.onload = function initDocument(){
ta = new TypeAhead(txtIn put, selStates);
}

</script>
</head>
<body>
<input id="txtInput" type="text"/>
<br />
<br />
<select id="selStates" size="5">
<option>Alabama </option>
<option>Arkansa s</option>
<option>Connect icut</option>
<option>Delawar e</option>
<option>Florida </option>
<option>Georgia </option>
<option>Hawai i</option>
<option>Indiana </option>
<option>Kentuck y</option>
</select>
</body>
</html>
Jul 23 '05 #1
2 1882
js****@scriptdu des.com wrote:
[...]
but I can't seem to get it to work.
But you're disgustingly close...

What am I missing? What do I need to do? [...] this.onNotInLis t = function(){void (0);}
//this.onNotInLis t = function(){aler t("not in list");} this.onNotInLis t = NotInList;
...

And that's it.
[...]
window.onload = function initDocument(){
ta = new TypeAhead(txtIn put, selStates);
}


Using global references to element id's only works in Firefox in
quirks mode (or IE always), use something like:

window.onload = function initDocument(){
ta = new TypeAhead(docum ent.getElementB yId('txtInput') ,
document.getEle mentById('selSt ates'));

Or better still, put your controls iside a form and use the
forms collection:

window.onload = function initDocument(){
ta = new TypeAhead(docum ent.aForm.txtIn put,
document.aForm. selStates));
....

<form action="" name="aForm">
<input name="txtInput" type="text"/>
<select name="selStates " size="5">
...
Or pass the IDs as strings and use gEBI from inside the
function and provide a document.all alternative for older IE.
Jul 23 '05 #2
js****@scriptdu des.com wrote:
I have the some HTML and JavaScript code that implements
a type-ahead capability for a text input control linked to a
select control (see end of message). The type-ahead
functionality works well. The only problem is that I'd like
to implement an external event sink so that when the text
control loses focus, I can do ... well whatever I might
happen to want the event sink to do.

The code below will even sink the onblur event of the text
control and will behave properly. (To see it in action,
change function for this.onNotInLis t to alert the user
when the text control loses focus.)

The problem is, I'd like to be able to set the notinlist sink
to a function outside the class. I'm not able to get the
syntax right for it. I've tried, for example,

var ta = new TypeAhead();
ta.onNotInList = pointerToMyNotI nListHandler;

but I can't seem to get it to work.

What am I missing? What do I need to do?

javacript dude
<html>
<head>
<script language="javas cript">
function TypeAhead(txtPa rent, selChild){
this.txtParent = txtParent;
this.selChild = selChild;
this.onNotInLis t = function(){void (0);}
//this.onNotInLis t = function(){aler t("not in list");}
var notInList = this.onNotInLis t;

this.txtParent. onkeyup = function(){
var optionText = txtParent.value .toUpperCase();
var options = selChild.option s;
for (var i = 0; i < options.length; i++){
if(optionText.l ength > 0 && options[i].text.toUpperCa se().indexOf(op tionText)==0){ options[i].selected=true;
return;
}
}
selChild.select edIndex = -1;

}

this.txtParent. onblur = function(){
if (selChild.selec tedIndex == -1) {
notInList();
}
}

this.selChild.o nclick = function(){
var selectedIndex = selChild.select edIndex;
if (selectedIndex > -1){
txtParent.value = selChild.option s[selectedIndex].text; }
}

}

TypeAhead.proto type.contructor = TypeAhead;

var ta;

function NotInList(){
alert("not in list");
}

window.onload = function initDocument(){
ta = new TypeAhead(txtIn put, selStates);
}

</script>
</head>
<body>
<input id="txtInput" type="text"/>
<br />
<br />
<select id="selStates" size="5">
<option>Alabama </option>
<option>Arkansa s</option>
<option>Connect icut</option>
<option>Delawar e</option>
<option>Florida </option>
<option>Georgia </option>
<option>Hawai i</option>
<option>Indiana </option>
<option>Kentuck y</option>
</select>
</body>
</html>


Had to google 'event sink' so you know you're dealing with a pro. #:o
Thought some closures might help (tested: NS7.2, ie6win, Opera 7).

<html>
<head>
<script type="text/javascript">

function TypeAhead(tfiel did, sfieldid)
{
var o = this;
this.tfield = document.getEle mentById(tfield id);
this.sfield = document.getEle mentById(sfield id);
this.onNotInLis t = function()
{
alert('not in list !');
this.sfield.sel ectedIndex = -1;
this.tfield.val ue = '';
this.tfield.foc us();
}
this.tfield.onk eyup = function()
{
o.tfield.value =
o.tfield.value. replace(/^\s+/,'').replace(/\s+$/,'');
if (/^\s*$/.test(o.tfield. value))
{
o.sfield.select edIndex = -1;
return;
}
var re = new RegExp('^' + o.tfield.value, 'i');
var options = o.sfield.option s;
for (var i = 0; i < options.length; i++)
{
if (re.test(option s[i].text))
{
options[i].selected = true;
return;
}
}
o.onNotInList() ;
}
this.sfield.onc lick = function()
{
var sI = o.sfield.select edIndex;
if (sI > -1)
o.tfield.value = o.sfield.option s[sI].text;
}
}

TypeAhead.proto type.contructor = TypeAhead;
var ta;

window.onload = function()
{
ta = new TypeAhead('txtI nput', 'selStates');
}

</script>
</head>
<body>
<input id="txtInput" type="text"/>
<br />
<br />
<select id="selStates" size="5">
<option>Alabama </option>
<option>Arkansa s</option>
<option>Califor nia</option>
<option>Connect icut</option>
<option>Delawar e</option>
<option>Florida </option>
<option>Georgia </option>
<option>Hawai i</option>
<option>Indiana </option>
<option>Kansa s</option>
<option>Kentuck y</option>
</select>
</body>
</html>

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6327
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334, column 13: there is no attribute "SRC" <bgsound src="C:\My Documents\zingwent.mids"> You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element. This error is...
1
2516
by: Jenny | last post by:
Need urgent help for an unsolved problem. In our ASP web application, we creat a Back button and if user click on this button, it execute history.go(-1) to go back to the previous page. All our users use IE 6.0 and most of them don't have problem with this button. But one user reported everytime he click on this button, there is a pop-up window shows up and ask him refresh and reload the page, after he clicks OK, the previous page will be...
1
291
by: John Hayes | last post by:
Hi, I am a complete newbie when it comes to javascript and I need some help. Our web site creates a text file which I would like the system to ask the user what to do with, either open, save or cancel. Currently we use the code below but that only puts it in the browser window and for the text files that is fine but some of the files are csv and that won't work. If I can't it to ask me what to do with it, can I force it to open in a new...
4
1735
by: Trip | last post by:
Please if someone can help me !!! I need client and server code(principle based on AJAX) for next problem: i have 3 <select> tags on html page.(it must be NO page reload(callback) only select(controles) regeneration !!!) In the first <select> goes countries, which must be pulled from some kind of database (whichever you want). after that if i select some country, second <select> must be filled with regions of that country, and when i...
9
2391
by: Mickey Segal | last post by:
The long-simmering Eolas patent dispute: http://www.microsoft.com/presspass/press/2003/oct03/10-06EOLASPR.mspx has led to an optional Microsoft Update: http://support.microsoft.com/kb/912945/en-us that creates non-JavaScript problems that can be fixed using JavaScript. With the Microsoft update installed, Java applets (as well as other content such as Flash videos) are unable to receive user input until an activating click or key press....
22
2332
by: the_grove_man | last post by:
I purchased a book titled "Pro ASP.NET 2.0" to get up to speed on web stuff because I ususally do Windows Form Applications.. But in the first chapters I was reading this week it brought to mind some things I heard in the past that I thought someone could clarify. On Page 6 it states "ASP.NET web pages (and web services) are executed within the CLR (common language runtime), so they can be authored in any language that has a...
4
3814
by: fredy | last post by:
I Need javascript help to retrive the cpu information of client in mozilla browser Note: <script language="javascript"> alert(navigator.cpuClass); </script>
1
1279
by: ttamilvanan81 | last post by:
Hai everyone, i am new to javascript. Now i have doing one Image gallary application. In that application i have upload two images, one for Befor image and another one for after image. All those images are stored into the filesystem, the images names only stored into the database. Left side of page: Before Pictures (One picture showing, date, description details). On picture shown (if there are other pictures, there is a button...
1
3138
by: saravanatmm | last post by:
I need javascript code for validate the email address. Email address field cannot allowed the capital letters, special characters except '@' symbol. But can allowed the small letters, numeric numbers. Now i use this script for validate the email address. But it allows the cpital letters otherwise its working correctly. SCRIPT FUNCTION ************************************************
10
7364
by: Dutchmarshalls | last post by:
Hi All, I'm using a Pay Pal form script, but locally tested in Dreamweaver with a browser it will do exactly what I'm aspect it to do. Only when I'm uploaded the file on the server it will give this ERROR. By th way, I'm using a Joomla 1.5.3 site. The URL: http://www.dutchmarshalls.com/index.php/service/51-service/87-price-and-product-list- The ERROR CODE:...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.