Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts

    attachEvent change style

    that suppose to bind onclick and change the backgroung color,
    this.style.backgroundColor doesnt work with IE, any solution?

    Code:
    <script type="text/javascript">
    var els = document.getElementsByTagName('input');
    for (i = 0; i < els.length; i++) {
        var el = els[i];
        if (el.addEventListener) {
            el.addEventListener ("click",changeBG,false);
        } else if (el.attachEvent) {
            el.attachEvent ("onclick",changeBG);
        }
    }
    
    function changeBG() {
        this.style.backgroundColor = 'red';
    }
    </script>

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Try this:
    Code:
    function changeBG(evnt) {
        var ev = (evnt) ? evnt : event;
        if (ev.srcElement) {
           ev.srcElement.style.backgroundColor = 'red';
        } else {
           ev.target.style.backgroundColor = 'red';
        }
    }
    I am still learning English

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thx a lot

  • #4
    New Coder
    Join Date
    May 2006
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hey, one more question, how do i use setInterval with this, i need the background to disappear after some time. i cant get it working.

  • #5
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    change your function like this:
    Code:
    function changeBG(evnt) 
    {
        changeBGColor(evnt,'red');
        window.setTimeout("changeBGColor("+evnt+",'white')",2000); //I added the comma between the two parameters inside the function call
        // the system will wait 2000 milli seconds before calling back the //method and setting the background color back to white
    }
    
    function changeBGColor(evnt,colorName)
    {
       var ev = (evnt) ? evnt : event;
        if (ev.srcElement) 
        {
           ev.srcElement.style.backgroundColor = colorName;
        } 
         else 
        {
           ev.target.style.backgroundColor = colorName;
        }
    }

  • #6
    New Coder
    Join Date
    May 2006
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you for your effort and time, i get this error:

    missing ] after element list

    ?


  •  

    Posting Permissions

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