Click here to Skip to main content
12,448,563 members (55,629 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Javascript
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.

<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 25-Apr-11 23:25pm
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'?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
SAKryukov 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-81 30-Apr-11 5:42am
   
Yes very good article My 5 too
Abhinav S 1-May-11 22:13pm
   
Thanks.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Thanks for the Q and here is tha ans
<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>
  Permalink  
Comments
swathi6589 9-May-11 7:56am
   
It Helped me a lot,Thank you Steven
Steven.Pinto 9-May-11 8:15am
   
welcome
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

another way of using this:

<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>
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 30 Apr 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100