Results 1 to 5 of 5

Thread: Coloring the fieldname when the cursor is placed in an input box

  1. #1
    Join Date
    Oct 2011
    Posts
    46
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Coloring the fieldname when the cursor is placed in an input box

    Hello,

    I need your help,

    How can I use javascript to automatically change the color of the field name (in red) above each of the input textboxes once the cursor has been placed or clicked in the textbox.

    Then if a new textbox is selected, it will revert the previous selected field to its default color (black) and color the newly selected field name?

    Code:
    <!DOCTYPE html>
    
    <html>
    
    <head></head>
    
    <body>
    
    <table border="0" cellspacing="0" width="100%" id="table1">
        <tr>
            <td>FIELD1</td>
            <td>FIELD2</td>
            <td>FIELD3</td>
        </tr>
        <tr>
            <td><input type="text" id="field1"/></td>
            <td><input type="text" id="field2"/></td>
            <td><input type="text" id="field3"/></td>
        </tr>
    </table>
    
    </body>
    
    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html>
    
    <html>
    
    <head></head>
    
    <body>
    
    <table border="0" cellspacing="0" width="100%" id="table1">
        <tr>
            <td>FIELD1</td>
            <td>FIELD2</td>
            <td>FIELD3</td>
        </tr>
        <tr>
            <td><input type="text" id="field1"/></td>
            <td><input type="text" id="field2"/></td>
            <td><input type="text" id="field3"/></td>
        </tr>
    </table>
    <script type="text/javascript">
    (function(){
    	var table = document.getElementById('table1'), inputs = table.getElementsByTagName('input'), names = table.rows[0].cells,
    	addEvent = (function(){return window.addEventListener? function(el, ev, f){
    			el.addEventListener(ev, f, false);
    		}:window.attachEvent? function(el, ev, f){
    			el.attachEvent('on' + ev, function(){f.call(el);});
    		}:function(){return;};
    	})(), i = inputs.length;
    	while(--i > -1){
    		inputs[i].setAttribute('data-index', i);
    		addEvent(inputs[i], 'focus', function(){
    			var i = names.length;
    			while(--i > -1){
    				names[i].style.color = '';
    			}
    			names[this.getAttribute('data-index')].style.color = 'red';
    		});
    	}
    })();
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    jason_kelly (05-09-2013)

  4. #3
    Join Date
    Oct 2011
    Posts
    46
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Wow!

    Crazy complicated coding...was definetly beyond my comfort level.

    As usual a job well done and a huge thank you.

    Please send the bill in the mail

    Cheers,

    Jay

  5. #4
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html>
    
    <html>
    
    <head>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function Field(id,n){
     var tr=document.getElementById(id),tds=tr?tr.getElementsByTagName('TD'):[];
     Field.lst?Field.lst.style.color='black':null;
     tds[n]?tds[n].style.color='red':null;
     Field.lst=tds[n];
    }
    /*]]>*/
    </script>
    </head>
    
    <body>
    
    <table border="0" cellspacing="0" width="100%" id="table1">
        <tr id="field" >
            <td>FIELD1</td>
            <td>FIELD2</td>
            <td>FIELD3</td>
        </tr>
        <tr>
            <td><input type="text" id="field1" onfocus="Field('field',0);" onblur="Field('field');"/></td>
            <td><input type="text" id="field2" onfocus="Field('field',1);"/></td>
            <td><input type="text" id="field3" onfocus="Field('field',2);"/></td>
        </tr>
    </table>
    
    </body>
    
    </html>
    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/

  6. The Following User Says Thank You to vwphillips For This Useful Post:

    jason_kelly (05-09-2013)

  7. #5
    Join Date
    Oct 2011
    Posts
    46
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Thanks again for your coding Vic.

    Light weight and versatile. I can understand exactly whats going on .... to a degree lol

    I guess there is more than one way to skin a cat!

    Cheers and have an awesome day.

    Jay.

Similar Threads

  1. Output Recordset to HTML Table with fieldname
    By jason_kelly in forum JavaScript
    Replies: 0
    Last Post: 01-25-2012, 06:05 PM
  2. Children's Coloring Program
    By rcw06 in forum General Paid Work Requests
    Replies: 1
    Last Post: 10-27-2011, 09:06 PM
  3. Replies: 0
    Last Post: 06-27-2011, 08:36 AM
  4. Magic Coloring Pages
    By rachelrose in forum Looking for such a script or service
    Replies: 7
    Last Post: 11-27-2008, 07:38 AM
  5. Replies: 0
    Last Post: 02-06-2006, 11:50 AM

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
  •