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 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    Remember Form Inputs on Blur, but excluding one field

    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.

    Code:
    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 :]

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Code:
    for (var i = 0; i < els.length; i++) {  
    if (i==15){continue;}
    //rest of loop
    maybe?

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Any solution based on a fixed numeric index will cause maintenance issues.

    If you give the element a unique ID:
    Code:
     if( el.type == 'text' && el.id != 'ignoreMe' )
     {
       ...
     }

  • #4
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    The input field has a unique ID...

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

    I tried

    Code:
    if(el.type == 'text && el.id != 'eda') {
       ...
       }
    But it didn't work

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by nicky View Post
    The input field has a unique ID...

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

    I tried

    Code:
    if(el.type == 'text && el.id != 'eda') {
       ...
       }
    But it didn't work
    I'd need to see that live.


  •  

    Posting Permissions

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