Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using three "LinkButtons" and a "Hidden Field". When user click on LinkButton, The Hidden value should be changed to LinkButton Text.So instead of using 3 different functions for OnClientClick, I am using a single work Function.
To achive my task I need to use "this" pointer in javascript.
I used "this" pointer in work() Function.

XML
<asp:LinkButton ID="lnkbProjects" runat="server"
        OnClientClick="work();">Home</asp:LinkButton>
    <asp:LinkButton ID="lnkbDocuments" runat="server"
        OnClientClick="work();">Section</asp:LinkButton>
    <asp:LinkButton ID="lnkbItems" runat="server"
        OnClientClick="work();" >Class</asp:LinkButton>
    <asp:HiddenField ID="hidlabel" runat="server" Value="I am old value" />

 <script type="text/javascript">
 function work()
 {
    document.getElementById('<%=hidlabel.ClientID%>').value=
    document.getElementById('<%=this.ClientID%>').innerText;
 }
</script>


When I tryed this, I am getting runtime error
"Microsoft JScript runtime error: Object required"
Posted
Updated 25-Apr-11 23:50pm
v4
Comments
Sandeep Mewara 26-Apr-11 5:29am    
Care to elaborate what, where and why you want to use a 'this'?

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 15:51pm    
Pretty clear short article, my 5.
--SA
Abhinav S 26-Apr-11 23:16pm    
Thank you for the 5, SA.
swathi6589 27-Apr-11 0:40am    
Good link,Thank you Abinav
Abhinav S 27-Apr-11 2:32am    
You are welcome. Vote if it helped.
Hemant__Sharma 30-Apr-11 5:42am    
Yes very good article My 5 too
Thanks for the Q and here is tha ans
XML
<asp:LinkButton ID="lnkbProjects" runat="server"
        OnClientClick="work(this);">Home</asp:LinkButton>
    <asp:LinkButton ID="lnkbDocuments" runat="server"
        OnClientClick="work(this);">Section</asp:LinkButton>
    <asp:LinkButton ID="lnkbItems" runat="server"
        OnClientClick="work(this);" >Class</asp:LinkButton>
    <asp:HiddenField ID="hidlabel" runat="server" Value="I am old value" />
 <script type="text/javascript">
 function work(getval)
 {
    document.getElementById('hidlabel').value=
    document.getElementById(getval.id).innerText;
 }
</script>
 
Share this answer
 
Comments
swathi6589 9-May-11 7:56am    
It Helped me a lot,Thank you Steven
Steven.Pinto2000 9-May-11 8:15am    
welcome
another way of using this:

XML
<asp:LinkButton ID="lnkbProjects" runat="server">Home</asp:LinkButton>
    <asp:HiddenField ID="hidlabel" runat="server" Value="I am old value" />

 <script type="text/javascript">
 document.getElementById('<%=lnkbProjects.ClientID%>').onclick = work;
 function work()
 {
    document.getElementById('<%=hidlabel.ClientID%>').value=
    document.getElementById(this.id).innerText;
 }
</script>
 
Share this answer
 
Comments
swathi6589 9-May-11 7:57am    
Thank u Hemant

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900