Tuesday, September 25, 2012

Select text, value, remove, selecteIndex on select element

1. Add options to the end of a select

$("#drpCountry").append('');

$("#drpCountry").append('');

or

$('').appendTo($("#drpCountry"));

$('').appendTo($("#drpCountry"));

2. Add options to the start of a select

$("#drpCountry").prepend('');

3. Replace all the options with new options

$("#drpCountry").html('');

4. Replace items at a certain index

$("#drpCountry option:eq(1)").replaceWith('');

$("#drpCountry option:eq(2)").replaceWith('');

5. Set the element at index 2 to be selected


$("#drpCountry option:eq(2)").attr("selected", "selected");

6. Set the selected element by text

$("#drpCountry").val("India").attr("selected", "selected");

7. Set the selected element by value

$("#drpCountry").val("2");

8. Remove an item at a particular index

$("#drpCountry option:eq(0)").remove();

9. Remove first item

$("#drpCountry option:first").remove();

10. Remove last item

$("#drpCountry option:last").remove();

11. Get the text of the selected item

var selectedtext=$("#drpCountry option:selected").text();

12. Get the value of the selected item

var selectedvalue=$("#drpCountry option:selected").val();

13. Get the index of the selected item

var selectedIndex=$("#drpCountry option").index($("#drpCountry option:selected"));

14. Alternative way to get the selected item

var selectedItem=$("#drpCountry option:selected").prevAll().size();

15. Insert an item in after a particular position

$("#drpCountry option:eq(0)").after("");

16. Insert an item in before a particular position

$("#drpCountry option:eq(3)").before("");

17. Getting values when item is selected

$("#drpCountry").change(function() {

var Selectedvalue=$(this).val();

var Selectedtext=$(this).children("option:selected").text();

});

Saturday, December 10, 2011

Fading image using jquery FadeTo


<script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
<script>
window.setInterval("Method()", 1000);
Method = function () {
var ff = parseFloat($("img").css("opacity")).toFixed(1);
// alert(ff);
if (ff == 0.6)
$("img").fadeTo("slow", 1);
else
$("img").fadeTo("slow", 0.6);
};
</script>


<img border="0" src="Images/Bald.jpg" width="400" />

Tuesday, December 6, 2011

Populate Select from ajax with jquery

<head>
<script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {

$.ajax({ url: "Fetchdata.ashx",
type: "GET",
dataType : 'json',
success: function (msg) {

$("#ddlcity").get(0).options.length = 0;
$("#ddlcity").get(0).options[0] = new Option("Select City", "-1");

$.each(msg, function (k, v) {
//alert(v);
$("#ddlcity").get(0).options[$("#ddlcity").get(0).options.length] = new Option(v.Name, v.ID);
});
},
error: function (msg) { alert("Error !!!!"); }
});
});
</script>

</head>
<body>
<select id="ddlcity"></select>
</body>


<%@ WebHandler Language="C#" Class="Fetchdata" %>

using System;
using System.Web;
using System.Collections.Generic;
public class Fetchdata : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
List Objcity = GetList();
System.Web.Script.Serialization.JavaScriptSerializer objJs = new System.Web.Script.Serialization.JavaScriptSerializer();
context.Response.Write( objJs.Serialize(Objcity));
}

public bool IsReusable {
get {
return false;
}
}
public List GetList()
{
List <Citydata> item = new List<Citydata>();
item.Add(new Citydata() { Name = "Delhi", Id = "1" });
item.Add(new Citydata() { Name = "Mumbai", Id = "2" });
item.Add(new Citydata() { Name = "Kolcatta", Id = "3" });
item.Add(new Citydata() { Name = "Chennai", Id = "4" });
item.Add(new Citydata() { Name = "Banglore", Id = "5" });
item.Add(new Citydata() { Name = "Agra", Id = "6" });
return item;
}
}

public class Citydata
{
string _Name = string.Empty;

public string Name
{
get { return _Name; }
set { _Name = value; }
}
string _Id = string.Empty;

public string Id
{
get { return _Id; }
set { _Id = value; }
}

}

ShareThis

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath