user521221
user521221

Reputation: 1

Regarding 'this' keyword in JavaScript

In my web page I've a Linkbutton with OnClientClick event as show below.

<asp:LinkButton ID="lnkbtn" Text="Click" runat="server" OnClientClick="dosomething(this.Text)" />

and I've defined the function as shown below in the head section of the web "page

<script type="text/javascript">
    function dosomething(ObjCntxt)
    {
        alert(ObjCntxt.toLocaleString());
        var textval = ObjCntxt;
        alert(textval.value);            
    }
</script>

When i run the page and click on the LinkButton i'm getting the message undefined.

I request you all kindly solve my problem.

Thanks & Regards.

Upvotes: 0

Views: 314

Answers (2)

Sukesh Marla
Sukesh Marla

Reputation: 177

Try this One

     <script type="text/javascript" language="javascript">
    function doSomething(ObjValue) {
        alert(ObjValue); // Text
       }
</script>   

    <asp:LinkButton ID="lnkbtn" Text="Click" runat="server" OnClientClick="doSomething(this.value);">Text</asp:LinkButton>

Upvotes: 0

abatishchev
abatishchev

Reputation: 100288

This works for me:

<script type="text/javascript" language="javascript">
    function doSomething(ObjCntxt) {
        alert(ObjCntxt); // Text
        alert(ObjCntxt.toLocaleString()); // Text
        alert(ObjCntxt.toString()); // Text

        alert(ObjCntxt.value); // undefiend
    }
</script>

<asp:LinkButton ID="lnkbtn" Text="Click" runat="server" OnClientClick="doSomething(this.text);">Text</asp:LinkButton>

Remember, that the content of doSomething is JavaScript, not .NET, so you should use JavaScript members, such as this.text not this.Text

What do you expect from ObjCntxt.value?? Christmas gift?

Upvotes: 1

Related Questions