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 6 of 6

Thread: display li

  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    display li

    Why isnt this working?

    Code:
    function display_form(obj) {
    	var button_name = obj.id.split("dialog_");
    	var t = document.getElementById(button_name[1] + '_form').style.visibility = 'visible';
    	alert(t);
    }
    IE is saying:
    Code:
    id is 'null' or not an object
    Last edited by martynball; 07-18-2010 at 08:41 PM.

  • #2
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by martynball View Post
    Why isnt this working?
    At the point that function is called, the reference passed to the parameter obj does not have a defined id property. You haven't shown enough code to give a more detailed answer.

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Code:
    function listener_loginForm() {	
    	addEvent(document.getElementById('input_username'), "click", add_username_password);
    	addEvent(document.getElementById('input_username'), "blur", remove_username_password);
    	
    	addEvent(document.getElementById('input_password'), "click", add_username_password);
    	addEvent(document.getElementById('input_password'), "blur", remove_username_password);
    	
    	addEvent(document.getElementById('dialog_login'), "click", login_dialog_login);
    	addEvent(document.getElementById('dialog_register'), "click", login_dialog_register);
    	addEvent(document.getElementById('dialog_recovery'), "click", login_dialog_recovery);
    }
    
    var add_username_password = function() { add_input_text(this); };
    var remove_username_password = function() { remove_input_text(this); };
    
    var login_dialog_login = function() { display_form(this); };
    var login_dialog_register = function() { display_form(this); };
    var login_dialog_recovery = function() { display_form(this); };
    
    //This function attaches events to elements. 
    var addEvent = function( elm, evt, fun ) {
    	if ( elm.addEventListener ) {
    		elm.addEventListener( evt, fun, false );
    	} else if ( elm.attachEvent ) {
    		elm.attachEvent( 'on' + evt, fun );
    	} else {
    		elm [ 'on' + evt ] = fun;
    	}
    };
    
    addEvent ( window, "load", listener_loginForm );
    
    //Put text in fields if there is not data, also switch text field to password field
    //when focused on. Switch back to text field if no text is entered. 
    function add_input_text(obj) {
    	var id = obj.id.split("input_");
    	document.getElementById(id[1]).innerHTML = '';
    }
    function remove_input_text(obj) {
    	if (obj.value.length == 0) {
    		var id = obj.id.split("input_");
    		var id2 = id[1].substr(0, 1).toUpperCase() + id[1].substr(1);
    		document.getElementById(id[1]).innerHTML = id2;
    	}
    }
    function display_form(obj) {
    	var button_name = obj.id.split("dialog_");
    	document.getElementById(button_name[1] + '_form').style.visibility = 'visible';
    }

  • #4
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    I.E.'s attachEvent does not preserve the this reference. Unless you're attaching more than one handler for the same event to an element, I suggest reducing addEvent to:
    Code:
    //This function attaches events to elements. 
    var addEvent = function( elm, evt, fun ) 
    {
    		elm [ 'on' + evt ] = fun;
    };

  • Users who have thanked RandomUser531 for this post:

    martynball (07-18-2010)

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    I don't understand you. so what is wrong with my code?

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Actually, if I do it this way:
    Code:
    function listener_loginForm() {	
    	addEvent(document.getElementById('input_username'), "click", add_username_password);
    	addEvent(document.getElementById('input_username'), "blur", remove_username_password);
    	
    	addEvent(document.getElementById('input_password'), "click", add_username_password);
    	addEvent(document.getElementById('input_password'), "blur", remove_username_password);
    	
    	addEvent(document.getElementById('dialog_login'), "click", login_dialog_login);
    	addEvent(document.getElementById('dialog_register'), "click", login_dialog_register);
    	addEvent(document.getElementById('dialog_recovery'), "click", login_dialog_recovery);
    }
    
    var add_username_password = function() { add_input_text(this); };
    var remove_username_password = function() { remove_input_text(this); };
    
    var login_dialog_login = function() { display_form('login'); };
    var login_dialog_register = function() { display_form('register'); };
    var login_dialog_recovery = function() { display_form('recovery'); };
    
    function display_form(form) {
    	var t = document.getElementById(form + '_form').style.visibility = 'visible';
    	alert(t);
    }
    it should work better, but for some reason IE says that an object is required. And FF does nothing. No alert.


  •  

    Posting Permissions

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