Reputation: 8646
I have textbox in asp.net as:
<asp:TextBox ID="txtLinkedTicket" runat="server" onBlur="javascript:saveChanges('txtLinkedTicket', document.getElementById('MainContent_txtLinkedTicket').Value);">
</asp:TextBox>
Onblur function in javascript:
function saveChanges(controlName, controlValue) {
alert(controlValue);
PageMethods.updateOnChange(controlName, controlValue, onSavingChange);
return false;
}
function onSavingChange(stringval) {
}
I am passing controlname and its value to function.
But i am not getting value of textbox when i do alert for it.
It shows me 'undefined'.
Have i passed value wrongly??
Please help me.
Note: I dont want to do :
document.getEleme.... in javascript function because there are 10 textboxes hence from each textbox i just wanted to send its value to function only.
Please help me.
Upvotes: 0
Views: 210
Reputation: 2370
Eshan is right.
The ASP.NET runtime auto-generates IDs based upon the value of the ClientIDMode property of the server side control.
Make it Static and it will not change the ID you assign and you can control client side code the way you want.
Upvotes: 2
Reputation: 62498
Firstly, set ClientIDMode= "Static"
as on page render, server may change the id for uniqueness :
<asp:TextBox ID="txtLinkedTicket" runat="server" ClientIDMode="Static" onBlur="javascript:saveChanges('txtLinkedTicket', document.getElementById('MainContent_txtLinkedTicket').Value);">
</asp:TextBox>
Secondly, jquery would be more better option to do it:
$('#txtLinkedTicket').blur(function(){
alert($(this).val());
});
Note: for using jquery you need to inclued jquery script file in your page.
In your page add jquery script:
<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
Upvotes: 2