0

i want when i click on my input to change the background color of the field. I would like to do it by calling a function . What am i doing wrong ? Thanks !

<input type="text" onFocus="makefieldred();" onBlur="makefieldwhite();" >


<script>
function makefieldred() {
    this.style.backgroundcolor='red' ;
    }
function makefieldwhite() {
    this.style.backgroundcolor='white' ;
    }
</script>
Pantelis10
  • 15
  • 1
  • 1
  • 6
  • 2
    Why use JavaScript at all? [CSS :focus](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus) and example: http://jsfiddle.net/kgfzb47h/ – epascarello Aug 19 '14 at 17:45
  • Yes i know it but i am learing javascript so i wanted to learn how to make it with js also ! :) – Pantelis10 Aug 19 '14 at 17:58
  • If you want to learn more about event handlers, I recommend to read http://www.quirksmode.org/js/introevents.html – Felix Kling Aug 19 '14 at 18:08

1 Answers1

3

this within your function calls isn't the element, because of the way you've hooked them up. Also, the property name is backgroundColor (capital C) rather than backgroundcolor:

If you want to use the onXyz attribute mechanism, the easiest thing is to pass the element into the function:

<input type="text" onFocus="makefieldred(this);" onBlur="makefieldwhite(this);" >


<script>
function makefieldred(elm) {
    elm.style.backgroundColor='red' ;
}
function makefieldwhite(elm) {
    elm.style.backgroundColor='white' ;
}
</script>

Alternately, use modern techniques to set up the event handlers:

<input type="text" id="the Input">


<script>
(function() {
    var input = document.getElementById("theInput");
    input.addEventListener("focus", makefieldred, false);
    input.addEventListener("blur", makefieldwhite, false);

    function makefieldred() {
        this.style.backgroundColor='red' ;
    }
    function makefieldwhite() {
        this.style.backgroundColor='white' ;
    }
</script>

If you have to support IE8, you'll need to use attachEvent if you don't see addEventListener on input. To handle that browser incompatibility, you can have a function to do it (taken from my other answer here):

var hookEvent = (function() {
    var div;

    // The function we use on standard-compliant browsers
    function standardHookEvent(element, eventName, handler) {
        element.addEventListener(eventName, handler, false);
        return element;
    }

    // The function we use on browsers with the previous Microsoft-specific mechanism
    function oldIEHookEvent(element, eventName, handler) {
        element.attachEvent("on" + eventName, function(e) {
            e = e || window.event;
            e.preventDefault = oldIEPreventDefault;
            e.stopPropagation = oldIEStopPropagation;
            handler.call(element, e);
        });
        return element;
    }

    // Polyfill for preventDefault on old IE
    function oldIEPreventDefault() {
        this.returnValue = false;
    }

    // Polyfill for stopPropagation on old IE
    function oldIEStopPropagation() {
        this.cancelBubble = true;
    }

    // Return the appropriate function; we don't rely on document.body
    // here just in case someone wants to use this within the head
    div = document.createElement('div');
    if (div.addEventListener) {
        div = undefined;
        return standardHookEvent;
    }
    if (div.attachEvent) {
        div = undefined;
        return oldIEHookEvent;
    }
    throw "Neither modern event mechanism (addEventListener nor attachEvent) is supported by this browser.";
})();

Then:

<script>
(function() {
    var input = document.getElementById("theInput");
    hookEvent(input, "focus", makefieldred);
    hookEvent(input, "blur", makefieldwhite);

    function makefieldred() {
        this.style.backgroundColor='red' ;
    }
    function makefieldwhite() {
        this.style.backgroundColor='white' ;
    }
</script>
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    I need to have my laptop to hand when I see these questions, not try and beat anyone via iPad... – David Thomas Aug 19 '14 at 17:45
  • 2
    I believe it needs to be [`elm.style.backgroundColor`](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Properties_Reference) with a capital "C". – showdev Aug 19 '14 at 17:45
  • @epascarello: ***Very*** good point, I was sort of looking at the general rather than the specific. (And it's not really all that much code, but if that [changing colors} is the only end goal, CSS would be the way to do it.) – T.J. Crowder Aug 19 '14 at 17:50
  • Thank you very much !!! I am just starting to learn javascript ! and yes the easiest way is by doing it in CSS but i just want to learn how javascript works ! :) – Pantelis10 Aug 19 '14 at 17:50
  • @InFamouStarlight: You're welcome, glad that helped. Do look at the CSS solution as well (epascarello linked info in a comment), but the above will help you for those situations where CSS can't do the job. – T.J. Crowder Aug 19 '14 at 17:51