SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Onblur not working

    Hi! I have:

    Code JavaScript:
           var box = document.createElement("input");
    	box.type ="text";
    	box.name ="modules[" + numOfDays + "]";
    	box.id = "modules[" + numOfDays + "]";
    	box.size = "3";
    	box.length = "3";
    	box.onblur = "alert('hello');";

    But the onblur does not work. The alert function is not called. Is there something wrong?

    Thanks.

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    box.onblur = function () {
        alert("hello");
    };

    The onblur property is an event handler and must be a function reference, not a string.

    You can also do,
    Code JavaScript:
    function myOnBlur() {
        alert("hello");
    }
     
    box.onblur = myOnBlur;
    Note that is mustn't be box.onblur = myOnBlur(), with brackets.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •