SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Fire Event on Second Attempt

    I am making a backspace function, but I only want the event to fire if the input field is empty and I hit backspace again. I don't want it to fire when I hit backspace and the field is empty simultaneously!

    If I type in... j then hit backspace, my event will fire.

    I want to type in... j then hit backspace, don't fire the event, hit backspace again, fire the event

    Code JavaScript:
    function backspace_delete(e) 
    {
    	if (!e) 
    	{ 
    		// IE reports window.event instead of the argument
    		e = window.event;
    	}
    	var keycode;
     
    	if (document.all) 
    	{
    		// IE
    		keycode = e.keyCode;
    	} 
    	else 
    	{
    		// Not IE
    		keycode = e.which;
    	}
     
    	if (keycode == 8 && with_field.value == '')
    	 {
    		if (document.all)
    		{
    			//IE
    			removeElementById(with_input.lastChild.id);
    		} 
    		else 
    		{
    			//Non IE
    			removeElementById(with_input.lastChild.id);
    		}
    	}
    }

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry not understanding what you are requiring.

    You have an input field (type="text"). Say it starts empty.

    You type in the three letters "abc" and then backspace. You want nothing to happen.

    You hit backspace a second time and the letter "c" is removed.

    You hit backspace a third time and the letter "b" is removed.

    Your type the letter "d" and then backspace. You want nothing to happen.

    You hit backspace a second time and the letter "d" is removed.

    You hit backspace a third time and the letter "a" is removed. Leaving the field empty.

    You hit backspace a fourth time. Nothing can happen so nothing does.

    In this scenario you could achieve the desired effect by everytime a character a,b,c,d, ... is typed setting a property, of you choice - say ignorebs, of the input element to true. If a backspace is detected and the property is true you ignore the keystroke and set the property to false so next time the backspace will action.


Bookmarks

Posting Permissions

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