Search code examples
javascriptasp.netparameterstooltip

send the value of tooltip to javascript function


how can i get the value of tooltip from this object to the javascript function LoadDiv() i created

this is the object:

<asp:Image ID="image1" runat="server" ImageUrl = '<%#"data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Data")) %>' onclick = "LoadDiv(this.src, this)" style ="cursor:pointer" ToolTip='<%# Eval("AdsID") %>' />

and this is the javascript function i created

function LoadDiv(url, lnk) {
            var img = new Image();
            var bcgDiv = document.getElementById("divBackground");
            var imgDiv = document.getElementById("divImage");
            var imgLoader = document.getElementById("imgLoader");
            var imgFull = document.getElementById("imgFull");
            var dl = document.getElementById("<%=rpImages.ClientID%>");
            var imgs = dl.getElementsByTagName("img");
}


Solution

  • The first thing to understand here is that the ASP code won't be available in that exact form to the JavaScript. It gets compiled on the server to standard HTML, and the JavaScript will operate on that HTML. The ToolTip attribute may not appear in the HTML as is; at a guess I'd say it probably gets rendered to the title and/or alt attributes. So, taking the code you provided as an example, it would render out the HTML thus:

    <img id="Image1" src="data:image/jpg;base64 ..." alt="TOOLTIP VALUE" title="TOOLTIP VALUE" onclick="LoadDiv(this.src, this)" style="cursor:pointer" /> 
    

    The first thing you should do is check your rendered HTML to see how it ends up--it may or may not match what I have there. Fire the page up in your browser, and then view source or open up the F12 tools. Once you know that, you should be able to pull it off the lnk parameter in your function (to which you've passed this).

    • If it's the title attribute, you can get it with lnk.title
    • If it's the alt attribute, you can get it with lnk.alt
    • If it's some other attribute, you can get it using lnk.getAttribute("attribute_name")

    It should be noted that MSDN has this to say on the subject:

    This property is rendered for all browsers. However, only Microsoft Internet Explorer will display this property as a ToolTip. All other browsers will ignore this property.

    This suggests that it renders as a ToolTip attribute, and that it only works in Internet Explorer. If that's the case, I'd recommend using a different approach to render it for better cross-browser support.