Tag Archives: Java Scripts

Javascript to check the maxlength.

Since IE 8 doesn’t support the attribute maxlength, we need to use java script to workout this functionality. We can write the script as follows.

<textarea id="clm_add_lin_dta" maxlength="80" name="clm_add_lin_dta" style="resize:none;" onchange="testLength(this,80)" onkeyup="testLength(this,80)" onpaste="testLength(this,80)"></textarea>

<script>
   function testLength(ta,maxLength ) {
        if (ta.value.length > maxLength) {
            ta.value = ta.value.substring(0, maxLength);
        }
    }
</script>

How to make an event to occur automatically in webpage using jquery?

We have to use this when we need to initiate an event where user doesn’t like to click or make an action. To do this, use the following function:

$('#id').trigger('change'); // to run onchange event
$('#id').trigger('click'); // to run onclink event

Similarly we can run any event.

How to get values from the URL using javascript?

When we are in need to get values from the URL using java script as we get them using $_REQUEST[‘name’]; in PHP, $q->param(‘name’); in Perl etc. we can use the following function to get the values in java script :

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
         vars[key] = value;
    });
    return vars;
}

To get the values we should call the above function as follows :

var val1=getUrlVars()["name1"];
alert(val1);

If the URL is http://ww.example.com/page.php?name1=value1&name2=value2&name3=value3, then the

var val1=getUrlVars()["name1"]; //value1
var val2=getUrlVars()["name2"]; //value2
var val2=getUrlVars()["name2"]; //value3

How to get the current URL using javascript?

The way to get the current URL using java script is as follows:

var thisUrl = window.location.href;
alert( thisUrl );

The output will be :

 http://www.example.com/page.php?name=value

How to get values from fileds which have same class name in jquery?

The way to do it is as follows:

var cpt= new Array();
$('.cpt_code').each(function() {
   console.log( $(this).val() );
   //alert($(this).val());
   cpt[x]=$(this).val();
   x++;
})