Sep
28

Simple jquery confirmation link

JavaScript          Trackback

If you want simple confirmation for link (usually for delete link in your applications) here is simple code you can use:

$('.confirm').click(function() {
return confirm("Are you sure?");
});

Everything you have to do after adding this piece of code is to add “confirm” class to link you want confirm before proceeding.


Sep
20

Get the index of the current element, jquery getEq

JavaScript          Trackback

If you want to grab index of some element you can use this function:

$.fn.getEq = function() { return ($(this).parent().children()).index(this); }

You can call it like this:

$("#someelementid").getEq();


Aug
08

Detect enter key pressed

JavaScript          Trackback

Do you want to detect if somebody pressed enter on your form (maybe to prevent form submitting when someone press enter key). Here is jquery code that can help you to detect enter key being pressed:

$('input').keyup(function(e) {
	if(e.keyCode == 13) {
		alert('Enter key was pressed.');
	}
});

If you want to disable Enter key submitting youw form you can use something like this:

$("form").bind("keypress", function(e) {
	if (e.keyCode == 13) return false;
});


Dec
05

Disable Enter key in the form using JavaScript

JavaScript          Trackback

Have an HTML form on your site and sometimes you are receiving incomplete submissions? Ever wonder why? I’ll tell you. Many times your visitors are hitting the ‘Enter’ key instead of the ‘Tab’ key, and pressing ‘Enter’ button is not taking them to next field but submits the form, and that is causing incomplete form submissions.
Place this JavaScript snippet between your HEAD tags:
Use this simple JavaScript snippet, put it before tag, and that should disable the ‘Enter’ key within your form.

<script type="text/javascript"> function noEnter(e) { var e=(e)?e:((event)?event:null); var field=(e.target)?e.target:((e.srcElement)?e.srcElement:null); if ((e.keyCode==13)&&(field.type=="text")) {return false;} } document.onkeypress = noEnter; </script>


Sep
10

Javascript leading zero string prototype

JavaScript          Trackback

When you are working with dates it is useful to have all dates formatted in a same way. For example, it is better to have all dates formatted like ‘2007-01-01’ then like ‘2007-1-1’. This is javascript prototype that can help you with this leading zeros:

<script type="text/javascript">
String.prototype.leadingZero = function(){
if (this.length == 1) return '0' + this; else return this;
}
//show how it works
test1 = '1';  // we need zero here
test2 = '02'; // don't need zero

document.write ('"' + test1.leadingZero() + '"<br>');
document.write ('"' + test2.leadingZero() + '"<br>');
</script>

</body>
</html>


top