www.webdeveloper.com
Results 1 to 5 of 5

Thread: javascript question.

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    29

    javascript question.

    hello.. im new in javascript.. just started learning actually.. i know the basics and i have a question..

    why doesnt this work?

    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>

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,644
    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.color.match("ff")||x.style.color.match("255")){   // Moz browsers use RGB  'rgb(255,0,0) == red
                 x.style.color='#000000';
              }
            else
              {
                 x.style.color='#ff0000';          // Change the style
              }
            }
            </script>
            <button type="button" onclick="myFunction()">Click Me!</button>
    
            </body>
            </html>
    
        Quick reply to this message Reply   Reply With Quote Reply With Quote   Multi-Quote This Message
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Jun 2011
    Posts
    29
    ohhh i now get it... so my code was correct.. i just didnt know about the moz issue

    thank you very much

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by telisjok View Post
    ohhh i now get it... so my code was correct.. i just didnt know about the moz issue
    Your code was not correct because it was asking the wrong question:
    Code:
    if (x.style.match("ff"))
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  5. #5
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,644
    see also

    x.style.color='#000000';
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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