#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Simple problem. need help


    hello im new in this forum, also new in javascript..

    i have this code

    Code:
        <!DOCTYPE html>     
    <html>     
    <body>      
    
    <h1>My First JavaScript</h1>      
    
    <p id="demo" style="color:#000000;">     JavaScript can change the style of an HTML element.     </p>      
    
    <script>     function myFunction()     
    {     
    x=document.getElementById('demo') // Find the element     
    
    if (x.style.match("ff"))       
    {          x.style="color:#000000";       }     
    
    else       
    {          x.style="color:#ff0000";          // Change the style       
    
    }    
    }     
    </script>     
    
    <button type="button" onclick="myFunction()">Click Me!</button>      
    </body>     
    </html>
    but somethings wrong and the color doesnt change... any suggestions please?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0
    When you use el.style, you get the style object of that element. To change the color, you need to access the color property of that object:
    el.style.color = '#000000';
    Or you can do el.style.cssText = 'color: #000000'; but you'll risk resetting other styles you gave to that element to their defaults.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    2
    Rep Power
    0
    nahh... i dont need to do that... i just had to add this in my if statement

    if (x.style.color.match("ff")||x.style.color.match("255"))

    Moz browsers use RGB 'rgb(255,0,0) == red

IMN logo majestic logo threadwatch logo seochat tools logo