...

View Full Version : Remember Form Inputs on Blur, but excluding one field



nicky
01-05-2012, 04:46 PM
I have a JavaScript that remembers the values of a form by creating cookies for each input element onblur.

I cannot figure out though how to exclude one input field from being remember.

It would be document.getElementsByTagName('input')[15]

Every time I try to add an if/else statement in using that line, the script doesn't work altogether.



window.onload = function() {
rememberFormInputs('rma', 'input-');
}

/**
* Set a cookie
* @param string cookie name
* @param string cookie value
* @param string cookie expiration counter in days
* @param string cookie path
* @param string cookie domain
* @param bool secure?
*/

function setCookie(name, value, expires, path, domain, secure) {
var today = new Date();
today.setTime(today.getTime());
if (expires) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date(today.getTime() + (expires));
document.cookie = name+"="+escape(value) +
((expires) ? ";expires="+expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}

/**
* Get a cookie value
* @param string cookie name
*/

function getCookie(name) {
var start = document.cookie.indexOf(name + "=");
var len = start + name.length + 1;
if ((!start) && (name != document.cookie.substring(0, name.length))) {
return null;
}
if (start == -1) return null;
var end = document.cookie.indexOf(";", len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len, end));
}

/**
* Remebers form inputs after you fill them in
* @param string form id to remember fields
* @param string a prefix to prepend to all cookie names. (prevent naming conflicts)
*/

function rememberFormInputs(form_id, prefix) {
var form = document.getElementById(form_id);
var els = document.getElementsByTagName('input');
for (var i = 0; i < els.length; i++) {
var el = els.item(i);
if (el.type == 'text') {
el.onblur = function() {
var name = this.name;
var value = this.value;
setCookie(prefix + name, value, 90);
};
var old_value = getCookie(prefix + el.name);
if (old_value && old_value != '') {
el.value = old_value;
}
}
}
}


Does anyone have a solution? I would really appreciate it :]

xelawho
01-05-2012, 04:56 PM
for (var i = 0; i < els.length; i++) {
if (i==15){continue;}
//rest of loop

maybe?

Logic Ali
01-05-2012, 05:59 PM
Any solution based on a fixed numeric index will cause maintenance issues.

If you give the element a unique ID:

if( el.type == 'text' && el.id != 'ignoreMe' )
{
...
}

nicky
01-05-2012, 06:47 PM
The input field has a unique ID...

<input type="text" id="eda" name="eda"/>

I tried



if(el.type == 'text && el.id != 'eda') {
...
}


But it didn't work :(

Logic Ali
01-05-2012, 07:26 PM
The input field has a unique ID...

<input type="text" id="eda" name="eda"/>

I tried



if(el.type == 'text && el.id != 'eda') {
...
}


But it didn't work :(
I'd need to see that live.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum