Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I used the following script (based on articles I found here) for preventing postback on all textboxes when enter key is pressed. (except search textbox).
 <script type="text/javascript">
     $(function () {
         $(':text').bind('keydown', function (e) {
             //on keydown for all textboxes
             if (e.target.className != "searchtextbox") {
                 if (e.keyCode == 13) { //if this is enter key
                     e.preventDefault();
                     return false;
                 }
                 else
                     return true;
             }
             else
                 return true;
         });
     });
</script>


I'm using a content page and I added the script on ContentPlaceHolder, but it doesn't seem to prevent postback.

Are there more settings I need to setup in order the script to work?

Thank you in advance.
Posted
Comments
KaushalJB 15-Sep-15 2:13am    
Did you check that on "keyup" ?
Chriz12 15-Sep-15 2:47am    
instead of keydown? Yes, i tried it now but it doesn't work either.

Thank you for your answer.
F-ES Sitecore 15-Sep-15 6:18am    
You'll need to learn to do some basic debugging. Is your method called? How many elements does $(':text') return? is your keydown event called? What is the classname of the target? What is the keycode?

We can't run your code in your context, we don't know what it is. Google how to use the "debugger;" keyword with your browser dev tools and use that to step through your code and the solution might become apparent.

1 solution

Did you try keypress?
Keypress works for me.

Consider using the .not selector to omit .searchtextbox
Something like this:
JavaScript
$(document).ready(function(){
    $(':text').not('.searchtextbox').bind('keypress', function (e) {
		if (e.keyCode === 13) {
			//put code here for when enter is pressed
		}
	});
});


That should work, hope it helps out :)
 
Share this answer
 
Comments
Chriz12 16-Sep-15 1:30am    
Thanks for the answer, but it still doesn't work for me.
I added:

if (e.keyCode === 13)
{
e.preventDefault();
return false;
}
Is it correct?
jaket-cp 17-Sep-15 3:54am    
That looks okay to me.
It may not be a javascript issue, it could be an issue with the aspx markup or something.
Try and debug as "F-ES Sitecore" has suggested - he knows what he is talking about.
Or you could try using alert or console.log for a quick test to check if the javascript code is doing what you expect.
Here is a quick knocked up fiddle for a simple test http://jsfiddle.net/7kfzwcns/
Chriz12 18-Sep-15 1:20am    
Thank you for the answer. After a few tests I think the problem is because of Master/Content pages. I'm using the code in a content page.

When I used the code in a new webform it worked.

Any suggestions, what might be wrong?
Thank you.
jaket-cp 21-Sep-15 4:08am    
Not sure, it is hard to say.
Here are a couple of suggestions you could try.
Firstly, figure out if the bind javascript is being executed at all, try debug or the alert/console.log method (put just before the binding).
If it is not being executed, then as you have know, there is an issue with the Master/Content page (probably some other javascript being used in the Master page, or possibly a lack of javascript library).
In any case you need to figure out what the issue is.
What you could do is create a content page with no code-behind and minimal textbox and search textbox to use the Master page (or a copy of the Master page).
If it works then, the problem exists in the original content page, but if it does not, then assume it is in the Master page.
In either case, the next step would be to add/remove components from the offending page (be it content or master) and test on each iteration change to see if it works in the web browser.
Ensure you do a hard-refresh in the web browser, so you hopefully know what is being tested is what you have changed.
http://refreshyourcache.com/en/cache/
http://wiki.scratch.mit.edu/wiki/Hard_Refresh
I believe in most windows web browsers, Ctrl+R will work for a hard-refresh.
Hope that helps out :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900