javascriptoffsetserver-side-controls

Unable to retrieve offset for a ASP.NET server-side control


I'm trying to retrieve the offsetHeight of a server-side control but it gives me an error. Here's the following code snippet -

  function Test() {
            var imgFavorite = $("<%= imgFavorite.ClientID %>"); //imgFavorite is a server-side asp:Image control.
            alert(imgFavorite); //[object Object]
            alert(imgFavorite.offsetHeight()); //undefined.    
   }

What is wrong with the code?


Solution

  • You've missed the hash when declaring the jQuery object using an ID:

    var imgFavorite = $("#<%= imgFavorite.ClientID %>");