Thursday, February 24, 2011

How to find the active element id using jquery?

In many cases you wanted to know the element ID which is having the current focus. How will find out with out binding the focusin function.
Here is the solution:
Declare the jquery extended function like
jQuery.extend(jQuery.expr[':'], {
    focus: function (element) {
        return element == document.activeElement;
    }
});

The above function returns the active element details .
 
You can access the active element by using the ":" method. For example 
$(":").attr("id") . This statement will return the id of the element which is having the cursor position.
same way you can find which input textbox is active now.($("input:text:focus").attr("id")).

That's all you can able to find the active element id. 
Enjoy!!!!!!!!

Tuesday, February 8, 2011

How to find out the value of the server side control using jquery?

It happens many time that we do have the controls which are declared with runat="server". Accessing the value of those control is not so easy. For example


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>

<script src="jquery.js" type="text/javascript"></script>

</head>
<body>
<form id="form1" runat="server">
<div>
<input runat="server" id="TextBox1" type="text" /><br />
<asp:Button ID="TestButton" runat="server" Text="Button" />
</div>
</form>
</body>

<html>


We can access or set the value like this:
<script type="text/javascript">
$(function() {
$('#<%= TestButton.ClientID %>').click(function(e) {
var testValue = 'Test Value';
$('#<%= TextBox1.ClientID %>').val(testValue);

e.preventDefault();
});
});
</script>



Every control which is declared at server side will have different id assign by the framework.For accessing those control we need to use the clientID along with the control id.