js help

Discussion in 'OT Technology' started by P07r0457, Jul 21, 2007.

  1. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    I have many forms that I would like the text and password input fields to "highlight" when they have focus, and be normal when they do not.

    I have the following js code that work perfectly in opera. Any ideas why it does not do anything in Firefox or MSIE? I know it's because Opera follows the standards and FF/MSIE do not... But how can I fix it so that it works in all three?

    Code:
    window.onload = function()
    {
    	// Add our event handlers to our inputs:
    	var inputs = document.getElementsByTagName("input");
    	var count = inputs.length;
    	
    	for (var i=0; i<inputs.length; i++)
    	{
    		switch (inputs(i).type)
    		{
    			case "text":
    			case "password":
    				inputs(i).tag = inputs(i).className;
    				inputs(i).onfocus = "getFocus(this.name)";
    				inputs(i).onblur = "loseFocus(this.name)";
    				break;
    			default:
    		}
    	}
    
    }
    
    function getFocus(x)
    {	
    	document.getElementsByName(x)[0].className = document.getElementsByName(x)[0].tag + ' getFocus';
    }
    
    function loseFocus(x)
    {
    	document.getElementsByName(x)[0].className = document.getElementsByName(x)[0].tag + ' loseFocus';
    }
     
  2. piratepenguin

    piratepenguin New Member

    Joined:
    Jun 18, 2006
    Messages:
    1,067
    Likes Received:
    0
    Location:
    Ireland
    Firefox DEFINES Javascript, I'll tell you that much.
     
  3. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    okay, well it defines it in a web non-compliant way according to w3. Opera, however, follows the standard.


    Now if I recall correctly, you simply trolled my last thread of this nature, so please kindly offer a fix, or simply leave it alone.
     
  4. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
    There's a couple of problems. 1) You're referencing elements in the 'inputs' array using a function call syntax. You should use inputs instead. 2) When you reference "onfocus" and "onblur" in Javascript, you're referencing the actual event handler, not the text contents of the HTML element's "onfocus" and "onblur" attributes. You need to assign a function to the event handler. It won't (or shouldn't) understand a string value.

    Code:
    window.onload = function()
    {
    	// Add our event handlers to our inputs:
    	var inputs = document.getElementsByTagName("input");
    	var count = inputs.length;
    	
    	for (var i=0; i<inputs.length; i++)
    	{
    		switch (inputs[i].type) 
    		{
    			case "text":
    			case "password":
    				inputs[i].tag = inputs[i].className;
    				inputs[i].onfocus = new Function("document.getElementsByName(this.name)[0].className = document.getElementsByName(this.name)[0].tag + ' getFocus';");
    				inputs[i].onblur = new Function("document.getElementsByName(this.name)[0].className = document.getElementsByName(this.name)[0].tag + ' loseFocus';");
    				break;
    			default:
    		}
    	}
    
    }
    
    
     
  5. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    thank you :bowdown:
     
  6. piratepenguin

    piratepenguin New Member

    Joined:
    Jun 18, 2006
    Messages:
    1,067
    Likes Received:
    0
    Location:
    Ireland
    I cannot figure out what you are trying to say here.

    "a web non-compliant way according to w3" The w3 have little to do with Javascript. That isn't a huge problem only for the fact that Mozilla prepare and release good documentation in much the same way as w3 AND release reference implementations under free licenses.

    There's ECMAScript and w3 events and DOM functions and crap, too, and we all try to pick them up. What comes to Javascript though, Opera pick and chose what they want from Mozilla's documentation and IE's extensions. For example, Javascript has a 'const' keyword for defining constants, Opera has no clue of it. Skipped it. http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:const
    Your original post was more than a question.
    If you did not say this, I would not have posted. Do you understand that?

    I wasn't going to have time to help you. Perhaps if you posted a link to the page I could've pulled out a good Firefox JS debugger and enjoyed that, but no.

    edit: fuck, Firebug is at 1.0 now. http://www.getfirebug.com/ That is sweet.

    http://developer.mozilla.org/en/docs/JavaScript
     
    Last edited: Jul 22, 2007

Share This Page