Event question

Discussion in 'Javascript' started by sheldonlg, Jul 20, 2011.

  1. sheldonlg

    sheldonlg Guest

    i have a control where if I type in a few characters it brings up a
    select list. To illustrate, suppose I type in 123. It would then bring
    up a select list of 12301, 12302, etc.

    Supposing then I select one of them, say 12305. The control then shows
    12305. I also have an onChange event on that control. When that fires
    it looks for the value that is currently in that control. What it finds
    is not 12305, but 123.

    I am wondering is there is some other event that I can use that will
    fire when the value changes, but after it has been set to the new value.
    That is what I thought that onChange did, but apparently not.
     
    sheldonlg, Jul 20, 2011
    #1
    1. Advertisements

  2. sheldonlg

    sheldonlg Guest

    Answered my own question. It is the onSelect.
     
    sheldonlg, Jul 20, 2011
    #2
    1. Advertisements

  3. `onchange' and `onselect' are event-_handlers_. The corresponding events
    are `change' and `select'. MSDN Library has that mostly wrong for
    historical reasons.

    The `change' event occurs when a control loses the input focus and the
    control's value has changed while it had the input focus. The `select'
    event occurs "when the user selects some *text*"¹.

    Since there is no `onChange' or `onSelect' property, you must be using
    event-handler attributes. Be sure to declare HTML5 then, because there is
    no `onselect' attribute for the `in Valid HTML 4.01.

    However, even HTML5 does not redefine the `select' event, which indicates
    that you are using an error-prone approach. You should add listeners for
    mouse and keyboard events instead: `click' or `mousedown', `keyup' and
    `keypress'.


    PointedEars
    ___________
    ¹ <http://www.w3.org/TR/DOM-Level-3-Events/#event-type-select>
     
    Thomas 'PointedEars' Lahn, Jul 20, 2011
    #3
  4. `onchange' and `onselect' are event-_handlers_. The corresponding events
    are `change' and `select'. MSDN Library has that mostly wrong for
    historical reasons.

    The `change' event occurs when a control loses the input focus and the
    control's value has changed while it had the input focus. The `select'
    event occurs "when the user selects some *text*"¹.

    Since there is no `onChange' or `onSelect' property, you must be using
    event-handler attributes. Be sure to declare HTML5 then, because there is
    no `onselect' attribute for the SELECT element in Valid HTML 4.01.

    However, even HTML5 does not redefine the `select' event, which indicates
    that you are using an error-prone approach. You should add listeners for
    mouse and keyboard events instead: `click' or `mousedown', `keyup' and
    `keypress'.


    PointedEars
    ___________
    ¹ <http://www.w3.org/TR/DOM-Level-3-Events/#event-type-select>
     
    Thomas 'PointedEars' Lahn, Jul 20, 2011
    #4
  5. sheldonlg

    sheldonlg Guest

    Sorry to be sloppy with my terminology, but I;m glad you got the meaning.
     
    sheldonlg, Jul 21, 2011
    #5
  6. sheldonlg

    RobG Guest

    Have you discovered that IE dispatches a select event each time the
    selection changes *while* the user is still in the act of selecting
    the text?

    I have found that behaviour to make the use of the select event
    virtually useless, much better to wait for some other event (mouseup,
    keyup, whatever suits) as Thomas suggested, then see if anything has
    been selected.

    The bubbling of select events is also different across browsers to the
    point of being unusable, e.g. setting a listener on the body will
    respond only to selections in input and textarea elements in Firefox
    and others, but only in the document itself in IE. Such opposing
    behaviour makes event delegation difficult.

    The result is that you must put a listener on each element you wish to
    monitor (if you choose to persist with using select).
     
    RobG, Jul 21, 2011
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.