CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2005
    Posts
    31

    Question Help : What's wrong with this HTML Code ?

    Code:
    <HTML>
    <HEAD>
    <TITLE>Testing</TITLE>
    </HEAD>
    <BODY>
    <H1 class="Main" style="color:red;">Testing
    <H1 onclick='document.classes.Main.all.color="green";'>sdsddsd
    </BODY>
    </HTML>
    when click on sdsddsd. the testing word should change color. why when I test it. it's show error ?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Help : What's wrong with this HTML Code ?

    Here are a few suggestions. I hope you don't mind.

    1. Don't forget to conclude your H1 tags.
    2. Try to learn hexidecimal colors for color precision.

    Here is your code revised. Your onclick call should give you an error because it makes no sense. Here is one that will work.

    Code:
    <HTML>
    <HEAD>
    <TITLE>Testing</TITLE>
    </HEAD>
    <BODY>
    <H1 style="color:#ff0000;">Testing</H1>
    <H1 onclick="this.style.color='#00ff00'">sdsddsd</H1>
    </BODY>
    </HTML>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    May 2005
    Posts
    31

    Re: Help : What's wrong with this HTML Code ?

    I mean when I click on 'sdsddsd' text the 'testing' text should be change color not the 'sdsddsd' text.

  4. #4
    Join Date
    Dec 2005
    Posts
    62

    Re: Help : What's wrong with this HTML Code ?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    
    <body>
    <p id="test-text">Testing</p>
    <p onclick="document.getElementById('test-text').style.cssText='color: green;'">Click Me</p>
    </body>
    </html>
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function changeCssText(el,txt){
    document.getElementById(el).style.cssText = txt;
    }
    </script>
    </head>
    
    <body>
    <p id="test-text">Testing</p>
    <p onclick="changeCssText('test-text','color: green; font-weight: bold;')">Click Me</p>
    </body>
    </html>

  5. #5
    Join Date
    May 2002
    Posts
    10,943

    Re: Help : What's wrong with this HTML Code ?

    degsy, your code works but don't make it so hard on yourself. You don't have to pass it through a function. This will just make extra work.

    Code:
    <HTML>
    <HEAD>
    <TITLE>Testing</TITLE>
    </HEAD>
    <BODY>
    <H1 id="test" style="color:#ff0000;">Testing</H1>
    <H1 onclick="document.getElementById('test').style.color='#00ff00'">sdsddsd</H1>
    </BODY>
    </HTML>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Dec 2005
    Posts
    62

    Re: Help : What's wrong with this HTML Code ?

    If you use a function you can use it with several elements.
    It's good for future proofing if you want to change the function to effect all elements.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured