Fire Event on Second Attempt

JavaScript related questions should go here.
Post Reply
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Fire Event on Second Attempt

Post by unemployment »

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

[syntax=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);
}
}
}
[/syntax]
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Fire Event on Second Attempt

Post by unemployment »

I solved this. I need to fire the event onkeypress and not onkeyup to get my desired result.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Fire Event on Second Attempt

Post by jacek »

Am I blind or are the two things in each of these blocks the same :?

[syntax=javascript] if (document.all)
{
//IE
removeElementById(with_input.lastChild.id);
}
else
{
//Non IE
removeElementById(with_input.lastChild.id);
}[/syntax]
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Fire Event on Second Attempt

Post by unemployment »

jacek wrote:Am I blind or are the two things in each of these blocks the same :?

[syntax=javascript] if (document.all)
{
//IE
removeElementById(with_input.lastChild.id);
}
else
{
//Non IE
removeElementById(with_input.lastChild.id);
}[/syntax]

Well I found an example code online that said I need to use that for cross browse compatibility. In actuality... this function fails in chrome. Any thoughts as to why?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Fire Event on Second Attempt

Post by jacek »

What does the code for the function removeElementById look like ?
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Fire Event on Second Attempt

Post by unemployment »

jacek wrote:What does the code for the function removeElementById look like ?


[syntax=javascript]function removeElementById(id) {
var element = document.getElementById(id);
element.parentNode.removeChild(element);
}[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Fire Event on Second Attempt

Post by jacek »

Well that should work, does the chrome JS debugger have anything to say about why it does not work ?
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Fire Event on Second Attempt

Post by unemployment »

jacek wrote:Well that should work, does the chrome JS debugger have anything to say about why it does not work ?


I solved it. It just didn't work because Chrome, IE, Safari don't support onkeypress.... I needed to use onkeydown.
Post Reply