www.webdeveloper.com
Results 1 to 7 of 7

Thread: document.getElementById("input_field").onblur doesn't work?

  1. #1
    Join Date
    May 2005
    Posts
    21

    document.getElementById("input_field").onblur doesn't work?

    Okay, probably a really simple question, but still: in order to avoid using <input id="input_field" onblur="stuff()" /> is it not possible to do this in your .js?

    document.getElementById("input_field").onblur = function() {
    blah blah blah
    }

    Thank you

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    Once your input element loads, you can use JS to execute a function that uses addEventListener (or attachEvent for IE) to add the onblur event listener function. That way, you don't have to put the onblur="stuff()" in your HTML. Information on both functions is available at QuirksMode, or you can look them up yourself
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  3. #3
    Join Date
    May 2005
    Posts
    21
    Okay, thanks. Yeah, that's also the only thing I was able to find. Is there a reason the standard method is not supported (like window.onload = function()) ?

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    Sorry, I skipped that part of your post. That method is supported, it's just an older method of event registration.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  5. #5
    Join Date
    May 2005
    Posts
    21
    Okay, thank you aj_nsc! I'll try to research more on addEventListener.
    Cheers!

  6. #6
    Join Date
    Dec 2005
    Posts
    2,984
    Wow, I'm missing a lot of stuff today. I totally missed the title of your post. If the code in the title of this thread doesn't work, it's because it's called before the element is loaded - this is most likely due to this code being called in some script being loading in your head tag....try moving your script to the end of your page (right before your closing body tag). This is getting to be common practice nowadays to prevent long page loading times due to increasingly common use of JS framework libraries. Hope that's clarified why the code in your title hasn't been working.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  7. #7
    Join Date
    May 2005
    Posts
    21
    Oh okay, I'll try that! I didn't think about that before, thanks for all the help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles