Category Archives: jQuery

How to submit a form to a new window in post method using javascript?

To submit a form to a new window in post method,

HTML

<form name="form_message_center_popup" id="form_message_center_popup" method="post" action="/folder/file.ext">
    <input type="text" name="user_name" id="user_name" value="abc"/>
    <input type="text" name="clientid" id="clientid" value="efg"/>
    <input type="button" onclick="form_submiter()" value="submit"/>
</form>

JavaScript

function customer_support_msg() {
    var customer_support = window.open("about:blank", "_form", "location=no, menubar=no, toolbar=no, fullscreen=yes, scrollbars=yes, resizable=yes, width=800, height=600, replace=true");
    document.forms["form_message_center_popup"].target='_form';
    document.forms["form_message_center_popup"].submit();
    return false;
}

Regular Expression for Email Validation

The regular expression for email validation is follows :

^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$

The JS script for email validation can be written as follows :

function email_validator(mail_id) {
    var regexp=/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$/;
    if (regexp.test(mail_id)) {
        alert("This is a valid Email Id!!!");
        return true;
    }
    else {
        alert("This is an invalid Email Id!!!");
        return false;
    }
}

How to get selected text from drop down list (select box) using jQuery

It can be done as follows :

$("#dropdownid option:selected").text();

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 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++;
})