PDA

View Full Version : display li



martynball
07-18-2010, 09:35 PM
Why isnt this working?



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:

id is 'null' or not an object

RandomUser531
07-18-2010, 10:22 PM
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.

martynball
07-18-2010, 10:23 PM
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';
}

RandomUser531
07-18-2010, 10:37 PM
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:

//This function attaches events to elements.
var addEvent = function( elm, evt, fun )
{
elm [ 'on' + evt ] = fun;
};

martynball
07-19-2010, 01:05 AM
I don't understand you. so what is wrong with my code?

martynball
07-19-2010, 01:09 AM
Actually, if I do it this way:


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.