Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to? An element inside a div gains/loses focus, div class gets swapped

    New to Javascript and I'm trying to accomplish this.. I'm wrapping text input and submit button form elements inside a div. When either the text field or the submit button gain focus, I want to swap the class on the parent div. The form elements themselves wouldn't change, but the wrapper div would, for example, gain and lose a border depending on whether any of its contents had focus.

    Any suggestions?

  • #2
    Regular Coder
    Join Date
    Oct 2006
    Posts
    206
    Thanks
    1
    Thanked 0 Times in 0 Posts
    You would have a way to identify the parent div (say, an id) and have your event handler change the class:


    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    >
    <
    html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <
    head>
            <
    title></title>
            
            <
    meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <
    meta http-equiv="content-style-type" content="text/css" />
            <
    meta http-equiv="content-script-type" content="text/javascript" />
            
            <
    link href="" rel="stylesheet" type="text/css" /><!-- External Styles -->
            
            <!-- 
    Internal Styles -->
            <
    style type="text/css">
            
    /* <![CDATA[  */
                
    * {
                    
    margin ;
                    
    padding ;
                }
                
                
    #parentDiv {
                    
    border 1px solid black ;
                }
                
                
    #parentDiv.childFocused {
                    
    border 5px solid red ;
                }
            
    /* ]]> */
            
    </style>
            
            <
    script src="" type="text/javascript"></script><!-- External Script -->
            <!-- Should be: <link href="" rel="script" type="text/javascript" /> -->
            
            <!-- Internal Script -->
            <script type="text/javascript">
            /* <![CDATA[  */
                
            /* ]]> */
            </script>
        </head>

        <body>
            <script>
                function $ ( id ) { return document.getElementById( id ) ; }

                function focusHandler()
                {
                    $( 'parentDiv' ).className += ' childFocused' ;
                }

                function blurHandler()
                {
                    $( 'parentDiv' ).className = $( 'parentDiv' ).className.replace( ' childFocused' , '' ) ;
                }

                var kids = $( 'form' ).childNodes ;
                for ( var i = 0 , max = kids.length , kid ; i < max ; i += 1 )
                {
                    kid = kids[ i ] ;
                    if ( !kid.addEventListener ) { break ; } // skip this one because the next stuff won't work
                    
                    kid.addEventListener(
                        'focus' ,
                        focusHandler ,
                        true
                    ) ;
                    kid.addEventListener(
                        'blur' ,
                        blurHandler ,
                        true
                    ) ;
                }
            </script>
            
            <div id="parentDiv">
                <form id="form" action="">
                    <input type="text" />
                    <input type="submit" />
                </form>
            </div>
        </body>
    </html> 
    That doens't seem to work when I try it...but it should give you (or someone else) a starting point.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    The above code looks a bit overkill.
    I tested this in ie6 and ff2:


    Code:
    .
    
    <html><head><style>
    .red {background-color:#FF0000}
    .blue { background-color:#0000FF}
    </style></head><body>
    
    <div class="red" id="parentDiv">
    	<form id="form" action="">
    		<input type="text"
    			onfocus="this.parentNode.className='blue'" 
    			onblur ="this.parentNode.className='red'" 
    		/>
    		<input type="submit" />
    	</form>
    </div></body></html>
    
    .


  •  

    Posting Permissions

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