Shalvin Interests

Tuesday, February 23, 2016

JavaScript Arrays

<script>
        var friends = [];
        friends[0] = 'Praseed';
        friends[1] = 'Pai'

        window.onload = function()
        {
              document.write(friends[0]);
        }
    </script>

Push for adding items to Array

<script>
        var friends = [];
        friends[0] = 'Praseed';
        friends[1] = 'Pai'
        friends.push('Vaisakh');

        window.onload = function()
        {
            document.write(friends[0] + "<br/>");
            document.write(friends[2]);
        }
    </script>

Pop


 <script>
        var friends = [];
        friends[0] = 'Praseed';
        friends[1] = 'Pai'
        friends.push('Vaisakh');
        friends.pop();

        window.onload = function()
        {
            document.write(friends[0] + "<br/>");
            document.write(friends[2]);
        }
    </script>


Iterating an Array
<script>
        var friends = [];
        friends[0] = 'Praseed';
        friends[1] = 'Pai'
        friends.push('Vaisakh');
        
        window.onload = function()
        {
            for (var i = 0; i < friends.length; i++) {
                document.write(friends[i] + "<br/>");
            }
        }
    </script>


Asp .Net JavaScript Example

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ArrayPushDisplayShalvin.aspx.cs" Inherits="ArrayPushDisplayShalvin" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
 <script>
     var Friends = ['Shalvin', 'Reny', 'Praseed'];
     window.onload = function () {
         SetFocusOnTextBox();
         Display();
     }
     function Display()
     {
         var lbl = document.getElementById("lblMessage");
         lbl.innerHTML = "";
         for (var i = 0; i < Friends.length; i++) {
             lbl.innerHTML += Friends[i] + "<br/>";
             //lbl.innerText += Friends[i] + "<br/>";
     }
         return false;
     }
     function Save()
     {
         var s = document.getElementById("txtName");
         Friends.push(s.value);
         Display();
         s.value = "";
         s.focus();
         return false;
     }

     function SetFocusOnTextBox()
     {
         var s = document.getElementById("txtName");
         s.focus();
     }

     function Delete() {
         Friends.pop();
         Display();
         return false;
     }
 </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        Name
        <asp:TextBox ID="txtName" runat="server" ClientIDMode="Static"></asp:TextBox>
    </p>
    <p>
        <asp:Button ID="btnSave" OnClientClick ="return Save()" runat="server" Text="Save " ClientIDMode="Static" />
&nbsp;
        <asp:Button ID="btnDeleteFromLast" runat="server" 
            OnClientClick ="return Delete()"
            ClientIDMode="Static" Text="Delete from Last" />
&nbsp;
        <asp:Button ID="btnDisplay" runat="server" Text="Display" 
            OnClientClick ="return Display()" ClientIDMode="Static" OnClick="btnDisplay_Click" />
    </p>
    <p>
        <asp:Label ID="lblMessage" runat="server" 
            ClientIDMode="Static"></asp:Label>
        <br />
    </p>
</asp:Content>

No comments:

Post a Comment