Click here to Skip to main content
11,707,470 members (59,467 online)
Rate this: bad
good
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
Edited 25-Apr-11 23:50pm
v4
Comments
Sandeep Mewara at 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 at 26-Apr-11 15:51pm
   
Pretty clear short article, my 5.
--SA
Abhinav S at 26-Apr-11 23:16pm
   
Thank you for the 5, SA.
swathi6589 at 27-Apr-11 0:40am
   
Good link,Thank you Abinav
Abhinav S at 27-Apr-11 2:32am
   
You are welcome. Vote if it helped.
Hemant-Sharma-81 at 30-Apr-11 5:42am
   
Yes very good article My 5 too
Abhinav S at 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 at 9-May-11 7:56am
   
It Helped me a lot,Thank you Steven
Steven.Pinto at 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 at 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
0 OriginalGriff 156
1 Sergey Alexandrovich Kryukov 145
2 Mika Wendelius 140
3 chainerlt 110
4 ppolymorphe 104
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,806
4 Mika Wendelius 3,706


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 30 Apr 2011
Copyright © CodeProject, 1999-2015
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