Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12499

Re: Super basic newbie question

From "Jukka K. Korpela" <jkorpela@cs.tut.fi>
Newsgroups comp.lang.javascript
Subject Re: Super basic newbie question
Date 2012-03-08 17:17 +0200
Organization A noiseless patient Spider
Message-ID <jjaij0$hcm$1@dont-email.me> (permalink)
References <aaa3f536-d041-47fd-9c0c-2b158d5b82ee@x7g2000pbi.googlegroups.com>

Show all headers | View raw


2012-03-08 16:51, Larry.Martell@gmail.com wrote:

> I have a very simple web page with a text entry field:
>
> <input type="text" id='filterValue" size=20>

There is a syntax error there: the id attribute value starts with the 
apostrophe ('), but where is the closing apostrophe? Your actual page 
might be OK in this respect. We cannot know because you did not post the 
URL.

> In my javascript function I want to get each keystroke from that
> field, and write it out. So I have:
>
> window.onload = function() {
>      document.getElementById("filterValue").onkeypress = function() {
>          var val = document.getElementById("filterValue").value;
>          document.write("<p>" + val +"</p>");
>      }
> }
>
> But when I load the page I get this in the web console:
>
> [07:12:13.071] document.getElementById("filterValue") is null

The error in id='filterValue" can have odd effects, depending on what 
comes next in the HTML source. But perhaps more probably, the _real_ 
error on the page is yet something else, such as 'filterValue"'.

Moreover, using document.write() is a no-no when you are in an event 
handler. The following is a working approach:

<input type=text id=filterValue size=20>
<p id=stuff>
<script>
window.onload = function() {
     document.getElementById('filterValue').onkeypress = function() {
         document.getElementById('stuff').innerHTML = this.value;
     }
}
</script>

It's not a bug that when you test this, the characters appear in a 
delayed manner. When you are handling a keypress event, the character 
has not yet been consumed so that it gives its contribution to the input 
element's content.


-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Super basic newbie question "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-03-08 06:51 -0800
  Re: Super basic newbie question Martin Honnen <mahotrash@yahoo.de> - 2012-03-08 16:04 +0100
    Re: Super basic newbie question "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-03-08 09:15 -0800
      Re: Super basic newbie question Mike Duffy <never@you.mind.com> - 2012-03-09 01:01 +0000
  Re: Super basic newbie question "Tom de Neef" <tdeneef@qolor.nl> - 2012-03-08 16:08 +0100
    Re: Super basic newbie question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-09 04:05 +0100
      Re: Super basic newbie question "Tom de Neef" <tdeneef@qolor.nl> - 2012-03-09 10:47 +0100
  Re: Super basic newbie question SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2012-03-08 16:10 +0100
    Re: Super basic newbie question "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-03-08 09:15 -0800
      Re: Super basic newbie question Dr J R Stockton <reply1210@merlyn.demon.co.uk> - 2012-03-09 21:39 +0000
        Re: Super basic newbie question SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2012-03-10 09:55 +0100
          Re: Super basic newbie question Swifty <steve.j.swift@gmail.com> - 2012-03-10 18:19 +0000
            Re: Super basic newbie question Tim Streater <timstreater@greenbee.net> - 2012-03-10 21:59 +0000
              Re: Super basic newbie question Swifty <steve.j.swift@gmail.com> - 2012-03-12 09:58 +0000
                Re: Super basic newbie question Tim Streater <timstreater@greenbee.net> - 2012-03-12 10:02 +0000
                Re: Super basic newbie question Swifty <steve.j.swift@gmail.com> - 2012-03-12 10:57 +0000
                Re: Super basic newbie question John G Harris <john@nospam.demon.co.uk> - 2012-03-12 15:39 +0000
                Re: Super basic newbie question John G Harris <john@nospam.demon.co.uk> - 2012-03-12 15:37 +0000
                Re: Super basic newbie question Swifty <steve.j.swift@gmail.com> - 2012-03-13 10:34 +0000
                Re: Super basic newbie question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-03-13 10:12 -0600
                Re: Super basic newbie question Tim Streater <timstreater@greenbee.net> - 2012-03-13 19:44 +0000
                Re: Super basic newbie question Gene Wirchenko <genew@ocis.net> - 2012-03-13 19:50 -0700
                Re: Super basic newbie question Gene Wirchenko <genew@ocis.net> - 2012-03-13 08:58 -0700
            Re: Super basic newbie question SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2012-03-11 00:04 +0100
              Re: Super basic newbie question Tim Streater <timstreater@greenbee.net> - 2012-03-10 23:16 +0000
              Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-12 16:41 -0700
                Re: Super basic newbie question Tim Streater <timstreater@greenbee.net> - 2012-03-13 19:46 +0000
                Re: Super basic newbie question Dr J R Stockton <reply1211@merlyn.demon.co.uk.not.invalid> - 2012-03-14 21:01 +0000
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-14 17:18 -0700
                Re: Super basic newbie question John G Harris <john@nospam.demon.co.uk> - 2012-03-15 20:16 +0000
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-15 14:36 -0700
                Re: Super basic newbie question Tim Streater <timstreater@greenbee.net> - 2012-03-15 23:41 +0000
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-15 16:49 -0700
                Re: Super basic newbie question John G Harris <john@nospam.demon.co.uk> - 2012-03-16 16:55 +0000
                Re: Super basic newbie question Tim Streater <timstreater@greenbee.net> - 2012-03-16 18:09 +0000
                Re: Super basic newbie question Dr J R Stockton <reply1211@merlyn.demon.co.uk.not.invalid> - 2012-03-16 19:47 +0000
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 18:27 -0700
                Re: Super basic newbie question Dr J R Stockton <reply1211@merlyn.demon.co.uk.not.invalid> - 2012-03-18 19:27 +0000
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-18 16:55 -0700
                Re: Super basic newbie question Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-03-17 11:35 +0100
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-17 09:42 -0700
                Re: Super basic newbie question Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-03-18 01:33 +0100
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-17 17:46 -0700
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-17 19:45 -0700
                Re: Super basic newbie question Tim Streater <timstreater@greenbee.net> - 2012-03-15 10:00 +0000
                Re: Super basic newbie question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-17 14:53 +0100
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-17 12:43 -0700
                Re: Super basic newbie question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-17 23:39 +0100
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-17 15:54 -0700
                Re: Super basic newbie question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-18 10:48 +0100
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-18 04:06 -0700
                Re: Super basic newbie question Tim Streater <timstreater@greenbee.net> - 2012-03-18 13:52 +0000
                Re: Super basic newbie question David Mark <dmark.cinsoft@gmail.com> - 2012-03-18 12:10 -0700
          Re: Super basic newbie question Dr J R Stockton <reply1210@merlyn.demon.co.uk> - 2012-03-11 19:02 +0000
  Re: Super basic newbie question "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-03-08 17:17 +0200
    Re: Super basic newbie question "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-03-08 09:17 -0800
  Re: Super basic newbie question Gene Wirchenko <genew@ocis.net> - 2012-03-08 08:44 -0800
    Re: Super basic newbie question "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-03-08 09:24 -0800
  Re: Super basic newbie question Denis McMahon <denismfmcmahon@gmail.com> - 2012-03-08 17:03 +0000

csiph-web