By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,566 Members | 1,730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,566 IT Pros & Developers. It's quick & easy.

document.getElementById(el) has no properties.

Markus
Expert 5K+
P: 6,050
I'm trying to implement an onfocus and onblur background-color changing. But, as per usual, it ain't jivin'.

Here's the function

Expand|Select|Wrap|Line Numbers
  1. function bg(el, col)
  2. {
  3.  
  4.   document.getElementById(el).style.backgroundColor = col;
  5.  
  6. }
  7.  
and here's how it's being called
Expand|Select|Wrap|Line Numbers
  1. onblur="bg(this, '#000')"
  2.  
Any idea why it's saying document.getElementById(el) has no properties?
May 26 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
document.getElementById() expects an ID string, so either use el.style... or pass the id to the function.
May 26 '08 #2

Markus
Expert 5K+
P: 6,050
Cheers for that.

I did as you suggested and changed it to el.style..

:)
May 26 '08 #3

hsriat
Expert 100+
P: 1,654
When you pass this as an argument to a function, it refers to the owner of the function. So in case a function is contained in an HTML tag, the owner is that HTML tag (DOM element to be exact).
Expand|Select|Wrap|Line Numbers
  1. <input onblur="bg(this, '#000')" . . . . >
  2.    ^                |
  3.    '----------------'
So you should have either used this.id as argument, or the better is to do el.style, as acoder said.
May 26 '08 #4

Markus
Expert 5K+
P: 6,050
When you pass this as an argument to a function, it refers to the owner of the function. So in case a function is contained in an HTML tag, the owner is that HTML tag (DOM element to be exact).
Expand|Select|Wrap|Line Numbers
  1. <input onblur="bg(this, '#000')" . . . . >
  2.    ^                |
  3.    '----------------'
So you should have either used this.id as argument, or the better is to do el.style, as acoder said.
Thanks for that elaboration there, hs. ;)
May 26 '08 #5

hsriat
Expert 100+
P: 1,654
Not a problem :)
I'm always ready for explaining something which I know, and as much as I know.
That's a different thing that according to this, I'm left with few things to explain.
May 26 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.