Results 1 to 10 of 10

Thread: CSS code... I THINK!

  1. #1
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation CSS code... I THINK!

    I need a css code or java-script (don't know exactly) to help me with my problem.

    I want this: when I click INSIDE an INPUT box, the background color to chage to RED for examble, and when I click outsite the INPUT box, the background color should change back to white.

    mmmmmmmm... can anyone help me with this!?

    Thanks!

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    document.onclick = function(e) {
      var ev = e || window.event;
      if((ev.target || ev.srcElement).tagName.toLowerCase() == "input")
        (ev.target || ev.srcElement).style.backgroundColor = "red";
      else
        for(var i = 0, e = document.getElementsByTagName("input"); i < e.length; ++i)
          e[i].style.backgroundColor = "white";
    };
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Thanks!

    Thanks... but can you... TELL me how to use it...

    Is this CSS or JAVA-SCRIPT!?

  4. #4
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    when used like this...
    Code:
    <style type="text/css">
    
    document.onclick = function(e) {
      var ev = e || window.event;
      if((ev.target || ev.srcElement).tagName.toLowerCase() == "input")
        (ev.target || ev.srcElement).style.backgroundColor = "red";
      else
        for(var i = 0, e = document.getElementsByTagName("input"); i < e.length; ++i)
          e[i].style.backgroundColor = "white";
    };
    
    </style>
    ... it doesn't do nothing.

    Maybe I didn't expressed myself right. I have a contact form on my webpage and when visitors fill in this the files and want the "actve" INPUT BOXES (the selected one) to CHANGE COLOR.

    Thanks again.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's Javascript. It should be placed inside a <script> element, preferably in the head.

    /EDIT: Well, if you'd said that before, it would have been easier

    Code:
    <script type="text/javascript">
    window.onload = function() {
      for(var i = 0, e = document.getElementsByTagName("input").toLowerCase(); i < e.length; ++i)
        if(e[i].className.indexOf("nohighlight") == -1) {
          e[i].onfocus = function() {
            this.style.backgroundColor = "red";
          };
          e[i].onblur = function() {
            this.style.backgroundColor = "white";
          };
        }
    };
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    It's Javascript. It should be placed inside a <script> element, preferably in the head.
    Code:
    <script type="text/JavaScript">
    
    document.onclick = function(e) {
      var ev = e || window.event;
      if((ev.target || ev.srcElement).tagName.toLowerCase() == "input")
        (ev.target || ev.srcElement).style.backgroundColor = "red";
      else
        for(var i = 0, e = document.getElementsByTagName("input"); i < e.length; ++i)
          e[i].style.backgroundColor = "white";
    };
    
    </script>
    Works perfect like this...
    Now, just one more ajustment.... ... if you can help,...

    If I click the 1st INPUT BOX the COLOR changes to RED, but when I click the 2nd INPUT BOX the COLOR on the 1st INPUT BOX doesn't CHANGE back to WHITE... can.. YOU DO THAT!?

    Thanks!

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Use the latest code I posted instead.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Use the latest code I posted instead.
    Well... when i use the "latest" code, it give me an erros in this line
    Code:
        if(e[i].className.indexOf("nohighlight") == -1) {
    The Error:

    Line 70 (The Line Above)
    Char 18
    Message: Object doesn't support this property or method.

    What now!?

  9. #9
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I've done it! Something more simple but effective...
    Look at this...

    onfocus="this.style.background='red'"
    onblur="this.style.background='white'"


    Example:
    Code:
    <input type="text" size="30" name="b_fullname" style="border: 1px solid #FF7A22" onfocus="this.style.background='pink'" onblur="this.style.background='#c0c0c0'">

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Perhaps you have to use ...className.toString().indexOf(... instead in IE.

    That's essentially what I was doing, but all at once
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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