Search This Blog

Friday, November 6, 2015

Some Jquery Concept Practically

how to format a Date in MM/dd/yyyy HH:mm:ss format in javascript or jQuery?

Type1:
new Date()
Thu Nov 05 2015 16:11:03 GMT+0530 (India Standard Time)


Type 2:
var d = new Date,
    dformat = [d.getMonth()+1,
               d.getDate(),
               d.getFullYear()].join('/')+' '+
              [d.getHours(),
               d.getMinutes(),
               d.getSeconds()].join(':');
alert(dformat)

Type 3:
var d = new Date();
alert(
    ("00" + (d.getMonth() + 1)).slice(-2) + "/" +
    ("00" + d.getDate()).slice(-2) + "/" +
    d.getFullYear() + " " +
    ("00" + d.getHours()).slice(-2) + ":" +
    ("00" + d.getMinutes()).slice(-2) + ":" +
    ("00" + d.getSeconds()).slice(-2)
);

how to select dropdown based on value using jQuery?

In this post, Set selected dropdown value in page Load

HTML Code:

<select id="Status" title="Status">
<option selected="selected" value="0">New</option>
<option value="3">ANSWERED</option>
<option value="7">ARCHIVED</option>
<option  value="2">ASSIGNED</option>
<option value="9">DEFERMENT REQUESTED</option>
</select>
Jquery:

<script type="text/javascript">

 

    $(document).ready(function() {

$("select[title^='Status'] option:selected").text("New")
});
</script>


How to validation mobile number/phone number Numeric character Only in jQuery ?

In this post , phone number validation in Javascript/Jquery validation
this field only allows numeric characters only.

1.When key is down not allow to enter any alphabet characters.

HTML:

<input type="text" title="num_Phone" id="txtphone"/>

JQuery:


$(document).ready(function () {

  $('input[title="num_Phone"]').bind("keydown", function (event) {
    if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
         
            (event.keyCode == 65 && event.ctrlKey === true) ||

   
            (event.keyCode >= 35 && event.keyCode <= 39)) {
         
              return;
        } else {
         
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault();
            }
        }
   });
});


PreSaveAction Javascript validation in newform.aspx Page


Add the script editor webpart.

Add the below code:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">

<script type="text/javascript">
function PreSaveAction(){
  var txtnumber = $('input[title="num_Phone"]').val();

   if (!isNaN(txtnumber)){      
   alert("Please enter a Number only");
$('input[title="num_Phone"]').focus();
   return false;
}

else{
   return true;
    }
 }
</script>

How do I dynamically set the selected option of a drop-down list using JQuery, Javascript and html?

SCRIPT

<script type="text/javascript">
$(function(){
$("#gender").val("Male").attr('selected','selected");
});
</script>

HTML
<select id="gender" selected="selected">
                            <option>--Select--</option>
                            <option value="1">Male</option>
                            <option value="2">Female</option>
                        </select>


How to change text of label attribute(for) in jquery?

Script:

<script type="text/javascript">
$(document).ready(function()
{
$("label[for*='test']").html("others");
});

</script>
Html
<label for="test_992918d5-a2f4-4962-b644-bd7294cbf2e6_FillInButton">others</label>

Before Coding

After Coding




Two dimensional array in jQuery


Code:
<script src="../_layouts/15/JQuery/mainjq.js" type="text/javascript"></script>


Keypoints:
1. mail_details array declartion
2.dynamically added list items 

<script type="text/javascript">
    $(document).ready(function () {
        try {
var mail_details = new Array();
var kl=0;
 $().SPServices({
                operation: "GetListItems",
                async: false,
                listName: "Mail",
                CAMLQuery: query,
                completefunc: function (xData, Status) {
                    $(xData.responseXML).find("z\\:row,row").each(function () {
                     
                        mail_details[kl] = new Array(8);
                        mail_details[kl][0] = $(this).attr("ows_ID");
                        mail_details[kl][1] = $(this).attr("ows_Date");
kl = kl + 1;
                    });
                }
            });
  } catch (e) {
            alert("Ready Error:" + e);
        }
     
    });

Dynamic bind onclick event argument passing using jQuery


Here onclick action:
onclick="changeevent(\'' +$(this).attr("ows_Title")+'\')"


Binded html tag:

$("#spancat").append('<li style="cursor:pointer" onclick="changeevent(\'' +$(this).attr("ows_Title")+'\')">'+$(this).attr("ows_Title")+'</li>');


How to know the loaded Jquery file version using JQuery

Open the console window of the browser.
please enter the below code:
jquery.fn.jQuery



How to get field values ECMA Script/Javascript using CSOM?


SharePoint client object model used to retrieve, update, and manage data in SharePoint 2013.


Types:
1.Server Object Model
2.Client Object Model
3.Javascript Library
4.Rest Service/OData Library

Loads the SP.Runtime.js and SP.js files by using the getScript function in jQuery.


Title – SP.ListItem.get_item('Title');

ID – SP.ListItem.get_id();

Url -SP.ListItem.get_item('urlfieldname').get_url()

Description – SP.ListItem.get_item('descriptionfieldname').get_description();

Current Version – SP.ListItem.get_item("_UIVersionString");

Lookup field – SP.ListItem.get_item('LookupFieldName’).get_lookupValue(); // or get_lookupID();

Created By – SP.ListItem.get_item("Author").get_lookupValue();

Modified by – SP.ListItem.get_item("Editor").get_lookupValue();

Choice Field – SP.ListItem.get_item('ChoiceFieldcolumnName');

Created Date – SP.ListItem.get_item("Created");

Modified Date – SP.ListItem.get_item("Modified"); -> case sensitive does not work with 'modified’

File  – SP.ListItem.get_file();

File Versions -  File.get_versions();.

Content Type – SP.ListItem.get_contentType();

Parent List – SP.ListItem.get_parentList();

Note:  ('LookupFieldName’).get_lookupValue() sometimes returns an array of data, especially when your lookup allows multiple values.
For this,you will need to iterate the array and get each value individually.

 SP.ListItem.get_item('LookupFieldName’)[0].get_lookupValue();

For Arrays:


       for(var i = 0; i < oListItem.get_item("LookupFieldName").length; i++)
 {
      alert(oListItem.get_item("LookupFieldName")[i].get_lookupId()); // or get_lookupValue()
 }


How to disable save and Cancel button newform.aspx or editform.aspx SharePoint page in jQuery?


<script type="text/javascript">
 $(document).ready(function() {

$("input[value='Save']").attr("disabled", true);
$("input[value='Cancel']").attr("disabled", true);
});
</Script>


Spservices get firstname and lastname and Current Date in Jquery?

 var fuser= $().SPServices.SPGetCurrentUser({fieldName: "FirstName",

debug: false}); var luser= $().SPServices.SPGetCurrentUser({fieldName: "LastName",

debug: false});

$("#loginuser").html(fuser   +luser);
$("#currentDate").html(new Date());

How to get selected value/Text of a drop down's item using jQuery?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="text/javascript">
$(document).ready(function(){ 
  
    alert($('#cmb_Type:selected').text());
 
});</script>
<select id="cmb_Type">
  <option value="1">Test 1</option>
  <option value="2">Test 2</option>
</select>
var dtype = $('select[Title^="cmb_Type"] option:selected').val();
var dtype = $('select[Title^="cmb_Type"] option:selected').text();
 $('#cmb_Type option:selected').val();
 $('#cmb_Type option:selected').text();


how to bind peoplepicker control field login user in Sharepoint newform.aspx

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="/SiteAssets/jquery.SPServices-2014.02.min.js"></script>

<script type="text/javascript">
 $(document).ready(function() {
 
  //Get the current user name
  var user= $().SPServices.SPGetCurrentUser();
 // alert(user);
  $("div[title='txt_Username']").text(user.split('|')[1]);
  var date=new Date();
$("#tdate").html(date);
$("input[title='txt_OriginalAuthor']").val(user.split('|')[1]);

  });

</script>

<input type="text" value="" maxlength="255" id="txt_OriginalAuthor" title="txt_OriginalAuthor" >

How do I get (or) bind a value of a span tag using jQuery?


This value bind based id

$('#tdate').text();
or

$('#tdate').html();


This value bind based id + <span> tag
$('#tdate span').text();
or

$('#tdate span').html();


Determined number of times when the user clicks ?

HTML

<button id="btnSave">Save Click</button>

<button id="btnSav">Save Click</button>


jQuery


 $("#btnSave").click(


            function (event) {


                for(var i=0;i

                            $("#btnSave").trigger('click');


                }


            }            


        );


No comments:

Post a Comment