SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Styling of DIV

    Hi guys,

    Having a little trouble with using Javascript to style a div "on the fly". I'm currently creating a sign up membership form in which I'd like the input text box's holding DIV to change colour depending on if the text box has been filled in. For example, I have written some Javascript code that will change the surrounding div to a yellow colour when the text box is 'onfocus', I have then made the JS check to see if the text box has been filled when 'onblur'. It then changes to red if nothing has been typed in the box or back to the default colour if something has. Happy! But...

    I'd like to use this piece of JS code on all of the input text fields in my form which would mean I'd have to re-type and adapt the code (adding IDs name1, name2, name3 etc) to the various different div IDs. Is there a way to use the 'this.style' trick to use the same code on all the divs without having to re-type (and make my JS file unnecessarily big)?

    Many thanks


    The JavaScript Code:
    Code:
    function changeBackground()
    {
        document.getElementById("name1").style.background="#FFFFe0";    
    }
    
    function checkBackground()
    {
        var x=document.forms["applicationForm"]["firstNameInput"].value;
            
            if (x=="")
      {
      document.getElementById("name1").style.background="#ffaaaa";    
      }
                    
            else
      {
      document.getElementById("name1").style.background="#e6e8e9";    
      }        
            
    }
    HTML Code:
    Code:
    <form id="applicationForm" action="appForm.php" method="post">
                        
                        
                        
                   <div id="name1"  class="inputHolder">
        
                           <div class="formTitle">
    
                              <p>First Name</p><p style="color:red;">&nbsp;*</p>
                                                    
                            </div><!--formTitle-->
                                    
                            <div class="formInput">
    
                                <input type="text" name="firstNameInput"  onfocus="changeBackground()" onblur="checkBackground()">
                                                    
                            </div><!--formInput-->
                          
                             </div><!--inputHolder-->
    
    
    
                    <div id="name2"  class="inputHolder">
        
                           <div class="formTitle">
    
                              <p>Surname</p><p style="color:red;">&nbsp;*</p>
                                                    
                            </div><!--formTitle-->
                                    
                            <div class="formInput">
    
                                <input type="text" name="surNameInput"  onfocus="changeBackground()" onblur="checkBackground()">
                                                    
                            </div><!--formInput-->
                          
                             </div><!--inputHolder-->
    
                             </form>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function changeBackground(id)
    {
        document.getElementById(id).style.background="#FFFFe0";
    }
    
    function checkBackground(ip,id)
    {
        var x=ip.value;
    
            if (x=="")
      {
      document.getElementById(id).style.background="#ffaaaa";
      }
    
            else
      {
      document.getElementById(id).style.background="#e6e8e9";
      }
    
    }
    
    /*]]>*/
    </script></head>
    
    <body>
            <form id="applicationForm" action="appForm.php" method="post">
    
    
    
                           <div id="name1"  class="inputHolder">
    
                                   <div class="formTitle">
    
                                      <p>First Name</p><p style="color:red;">&nbsp;*</p>
    
                                    </div><!--formTitle-->
    
                                    <div class="formInput">
    
                                        <input type="text" name="firstNameInput"  onfocus="changeBackground('name1')" onblur="checkBackground(this,'name1')">
    
                                    </div><!--formInput-->
    
                                     </div><!--inputHolder-->
    
    
    
                            <div id="name2"  class="inputHolder">
    
                                   <div class="formTitle">
    
                                      <p>Surname</p><p style="color:red;">&nbsp;*</p>
    
                                    </div><!--formTitle-->
    
                                    <div class="formInput">
    
                                        <input type="text" name="surNameInput"  onfocus="changeBackground('name2')" onblur="checkBackground(this,'name2')">
    
                                    </div><!--formInput-->
    
                                     </div><!--inputHolder-->
    
                                     </form>
    
    </body>
    
    </html>

  3. #3
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help!


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
  •