Jump to content

keycode==13 and onkeyPress don't work on Firefox


breaststroke
 Share

Recommended Posts

Hello again! I am having a problem on firefox.I have an event (onkeyPress) on a textarea, which should trigger a function when the key pressed is Intro.I would have the following on HTML: HTML Code:..<textarea id="idtextarea" onkeyPress="showUser(this.value)"></textarea>... In javascript:

...function showUser(str){var keycode;if(window.event) keycode = window.event.keyCode;else if (e) keycode = e.which;else return true;if (keycode == 13){var xmlhttp;if(str=="")  {....

It works on the main browsers but not on Firefox. Nothing happens on this browser. I would appreciate any help.Thank you very much.Regards

Edited by breastsroke
Link to comment
Share on other sites

Have you checked whether the function is executing. You may have binded the event incorrectly. There are keydown and keyup events which may or may not be more appropriate for the situation.

Link to comment
Share on other sites

Hello Ingolme. Thank you.This is the HTML code related to the function:

<textarea cols="20" name="texto"rows="4" id="txt" onkeyPress="showUser(this.value)"></textarea></div>

the function works untill that part, but on Firefox.On the rest of browsers it works fine.According to my information keydown would seem more appropriate but i have tried with it as well and I have found the same problem.I have read somewhere onkeyPress and the code I have inside the function are a good option. Regards p.s.I have edited the javascript function on the previous post because I had duplicated the same code.As you suggest, if I include an alert (for example) just when the function starts it is okay, but not when I do it after: if (keycode == 13){Thank you again.

Edited by breastsroke
Link to comment
Share on other sites

the problem appear to be the second line, else if (e).

 function showUser(str){  var keycode;   if(window.event){	keycode = window.event.keyCode;  }else if (e){	keycode = e.which;  }else{	return true;  };};

window.event is for IE, and so that won't be true since you are using FF, so it will go the the next condition, which tests for e, which I am willing to bet is undefined. If it is undefined, then it will go to the last condition, which returns the function, thus bypassing the rest of your code. Typically the event is passed in as an argument to the function. you may want to consider something like this

<textarea id="idtextarea" onkeyPress="showUser()"></textarea> function showUser(e){  var keycode = (window.event) ? window.event.keyCode : e.keyCode;  var str = '';   if(keycode == 13){	var xmlhttp;	str = document.getElementById('idtextarea').value;	if(str==""){	};  };};

Most importantly is being able to debug these things by adding console or alert statements throughout your functions in order to track how your code is working and test for the values of your variables. Resourceshttp://www.quirksmod...nts_access.htmlhttps://developer.mo...M/event.keyCode

Edited by thescientist
Link to comment
Share on other sites

Hello thescientist. Thank you for your response and your explanations too. I have found another code which includes the e into de function.yours didn't work but i will try to analyze it so that I work it out.this is what I have made:

function showUser(str,e){var keycode=e.which?e.which:e.keyCode;if (keycode == 13){if(str==""){...

Thanks a million

Edited by breastsroke
Link to comment
Share on other sites

well, I wrote mine on the fly, so it mostly as a demonstration. that happens a lot here, which is why we also encourage people to be pro-active in learning how to debug their own code as well. but glad you got it to work though.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...