need Javascript OO help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    #1

    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>


  • Fred Oz

    #2
    Re: need Javascript OO help

    jsdude@scriptdu des.com wrote:
    [...][color=blue]
    > but I can't seem to get it to work.[/color]

    But you're disgustingly close...
    [color=blue]
    >
    > What am I missing? What do I need to do?[/color]
    [...][color=blue]
    > this.onNotInLis t = function(){void (0);}
    > //this.onNotInLis t = function(){aler t("not in list");}[/color]
    this.onNotInLis t = NotInList;
    ...

    And that's it.


    [...][color=blue]
    >
    > window.onload = function initDocument(){
    > ta = new TypeAhead(txtIn put, selStates);
    > }[/color]

    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.

    Comment

    • RobB

      #3
      Re: need Javascript OO help

      jsdude@scriptdu des.com wrote:[color=blue]
      > 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 &&[/color]
      options[i].text.toUpperCa se().indexOf(op tionText)==0){[color=blue]
      > 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 =[/color]
      selChild.option s[selectedIndex].text;[color=blue]
      > }
      > }
      >
      > }
      >
      > 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>[/color]

      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>

      Comment

      Working...