Jump to content

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


breaststroke

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

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.

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...