Archive for "Javascript"
Nov23jQuery way to limit input characters using javascript substring function
It’s an example of useful jQuery way to limit user entering a maximum set number of character in your input fields. Example makes use of javascript substring function to limit user upto 20 character which is triggered on keyup event. It truncate anything extra to that limit.
$("input[name=Title]").keyup(function(){
var text = $(this).val();
if(text.length > 20) {
text = text.substring(0, 20);
$(this).val(text);
}
})
Sep30Sum of values in input fields grouped by class attribute using jQeury
Here is the jQuery way to calculate the sum of all values stored in input fields grouped by class “.subtotal”. In this example the sum will be shown as an alert as soon as you leave a field after entering some numeric value in it Click to continue »
Jun08Fix to jQuery ui calendar not appearing from or hiding behind thickbox
If you are facing an issue of jQuery ui calendar not appearing while clicked from a jQuery thickbox or if some part of the calendar hiding behind the thickbox window you need to apply a quick fix to your calendar. Click to continue »
May18Object doesn’t support this property or method javascript error
In some browsers it would throw a “silent” error and stop executing javascript code. If you try to open page in IE in debug mode it would alert error message like this:
Object doesn’t support this property or method Click to continue »
Apr12jQuery pop up message box sliding from left to right and positioning horizontally at the center
Just wrote a simple sliding box script to make a message box appear sliding from left to the middle of web page. The box appears once a day. The first appearance is logged into a browser cookie, so if user opens the same website and if cookie was expired it appears again. Click to continue »
Mar04Validating php multiple-value checkbox form element with javascript
In php a multiple-value form element such as checkbox is named suffixed with a set of square brackets. For example,
My commonly used colors are:
<form action="index.php" name="myform" method="post" onsubmit="return validate_form();"> <input type="checkbox" name="colors[]" value="red" /> <input type="checkbox" name="colors[]" value="green" /> <input type="checkbox" name="colors[]" value="blue" />
To validate these kind of checkboxes’ using javascript is little tricky. While on the other hand, having a single checkbox without multiple-value selection it would have been straight forward, something like this:
function validate_form() {
// considering form element <input type="checkbox" name="color" value="red" />
if(true==document.myform.color.checked) {
//checked, ok. Let it go.
} else {
//not checked, stop it.
}
}
Considering multiple-value selection the document.myform[].color.checked or document.myform[].color.length would throw javasript error. To make it work as expected the javascript code in your validate_form function will need some changes. Here we go.
function validate_form() {
var flag = false; //at the initial state we assume that nothing is checked
for(var i=0; i < eval('document.myform.elements["colors[]"].length') ; i++) {
if(true === eval('document.myform.elements["colors[]"]['+i+'].checked')) {
flag=true; //at least some thing is checked, we are good to let them go past this check
/*
//Hint: As a separated implementation somewhere though, if you wanted you could trigger the checked/un-checked status of your checkbox like the following:
//document.myform.elements["colors[]"][i].checked = false; //or true etc.
*/
}
}
if(true==flag) {
//checked (at least something), ok. Let it go.
//Hint: if you wanted that all your checkbox should have been checked check for the value of i variable
} else {
//not checked, stop it.
}
}
In line 3 of modified function above we use powerful eval function to evaluate the string passed to it. The eval function accepts any valid piece of javascript code and returns output value. In the passed code string to eval function we write normal (parent to child access) algorithm to access the elements property (and its element) of myform object which is a property of our web page (document) object.
Mar04Useful trim prototype (function) in core javascript
Below is the most commonly used trim prototype/function in javascript. Posting for reference.
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g,"")
}
You can place the above code (wrapped in <script></script>) inside <header> section of your web page or you can place it in an external javascript file which you include in the header section.
//how to use it example var my_string = " Hello trim me "; var my_string_trimmed = my_string.trim(); //my_string_trimmed now has "Hello trim me"
Very useful tool.
Jan04A simple javascript image slide show using setTimeout and jQuery
Just now i created a small slideshow. The slideshow uses some static images stored in a javascript object (array) and runs till the last image in this array. For example, the array: Click to continue »
Sep29Form DOM element not recognized (created) in ajax form in IE when placed with end tag missing
Skipping a form end tag </form> costed me almost a day (i had to go to bed abandoning the fixing for some time though;)). I had been using ajaxForm with jQuery to submit ajax form which was placed on a popup dialog box opened through ajax. The form worked well in Mozilla Firefox, Google Chrome, Safari but did not work in Internet Explorer 7 & 8 (IE7 & IE8 ; i didn’t test in other versions of IE but perhaps they would not work either). The IE simply would not create the <form> tag at all so if you try to alert $(“#formID”).length it would return always 0(zero). It started to work fine (in IE) when i placed </form> to mark the end of the form (i know it was a mistake to not to place it there though). Click to continue »
Aug20Fix to jQuery plugin does not work on ajax loaded content
I had been loading table records through ajax pagination using jquery. Some of the urls/links (View Details, Edit and Delete) in loaded content had been using thickbox to handle respective requests. Although, if i did’t use the ajax to load records these links would work fine and thickbox would open. But once i introduced ajax they suddenly stopped working. Click to continue »





