Saturday, May 23, 2015

Json Data Using Javascript loop

Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="city" onchange="loadSTDCode(this.value);">
<asp:ListItem Text="Select City" Value="" />
<asp:ListItem Text="Warangal" Value="0" />
<asp:ListItem Text="hyderabad" Value="1" />
<asp:ListItem Text="Delhi" Value="2" />
</asp:DropDownList>

<asp:DropDownList ID="stdcode" runat="server">
</asp:DropDownList>
</div>
<script type="text/javascript">
function loadSTDCode(cityid) {
var list = document.getElementById('<%=stdcode.ClientID %>');
var optlength = list.options.length;
for (var i = 0; i < optlength; i++) {
    list.remove(0);

}
var city = 'c' + cityid;

var arr = [{ "c1": "562,343,534", "c2": "79,123,124,967", "c3": "532", "c4": "183", "c5": "240", "c6": "80", "c0": "1521,1522,1520,151,1523"}];
for (var i = 0; i < arr.length; i++) {
    var obj = arr[i];
    for (var key in obj) {
        var attrName = key;

        var attrValue = obj[key];
        if (attrName == city) {

            var posi = attrValue.indexOf(",");
            if (posi == -1) {
                var opt = document.createElement("option");
                document.getElementById('<%=stdcode.ClientID %>').options.add(opt);
                opt.text = attrValue;
                opt.value = attrValue;
            }
            else {
                var mySplitResult = attrValue.split(",");
                for (var i = 0; i < mySplitResult.length; i++) {
                    var opt = document.createElement("option");
                    document.getElementById('<%=stdcode.ClientID %>').options.add(opt);
                    opt.text = mySplitResult[i];
                    opt.value = mySplitResult[i];
                }
            }
        }
    }
}
}
</script>
</form>
</body>
</html>


Bind Unbind Events In JQuery javascript

Explanation:
In the form I have a button with id “btnsubmit”. Below is the code.

<input id="btnsubmit" type="button" value="Submit" />

My requirement is I need to bind a function to the onclick event  of the button and unbind it whenever it is notnecessary. To achieve this first define the function like the below.

var testingfn=function() {
//your code here
};


To bind the click event to the button(btnsubmit) below code is used

$('#btnsubmit').click(testingfn);
           
To unbind the click event to the button(btnsubmit ) below code is used


$('#btnsubmit').unbind("click");

Add Remove Selected And Set Items In Select Drop Down List MVC

Explanation:
For example in my form I have a select option with id “seloption”. To add a item with value “option1” and with text “Option-1” below code is used.


var options = jQuery('#seloption').attr('options');          
options[options.length] = new Option('Option Name''option value'truetrue);

Now to remove the option with value “option1” below code is used.

jQuery("#seloption option[value=option1]").remove();

To check whether the option with value “option1” is present in the select below code is used

jQuery("#seloption option[value='option1']").length>0

If the option is present then it will return the index value of the option. Otherwise it will return -1. To make the option “option1” as a selected option below code is used.

jQuery("#seloption ").val("option1");

To get the selected value of the select below code is used.


var value = $('select#dropDownIdoption:selected').val();

Get All Form Element Values Using JQuery

Steps To Follow:

1.To get all elements( textbox,checkbox,radiobutton,textarea) values inside a div with id “parentdiv” below jQuerycode is used.

var outval = "";
$('#parentdiv input:checked,#parentdiv textarea,#parentdiv input[type=text]').map(function () {
    outval += this.value + ",";
}).get().join(',')
alert(outval);

2. If your parentdiv contains many divs inside it then you can use the below code to get the value.

var outval = "";
jQuery('#parentdiv > div input:checked,#parentdiv > div textarea,#parentdiv > input[type=text]').map(function () {
    outval += this.value + ",";
}).get().join(',')

alert(outval);