Search code examples
javascriptjqueryimagebuttonimageurl

Uncaught TypeError: Cannot set property 'src' of null for ImageButton


I've searched through similar questions but I didn't find something to work for me.

I want to change the ImageURL of an ImageButton on a certain condition (t>80), but what I get is Uncaught TypeError: Cannot set property 'src' of null. My <head> code is:

    $(document).ready(function () {
           //doing other stuffs
        setInterval(function () {

                         $(".industrial.tank.size.one").each(function () {
                            $(this).industrial(t);

                            if (t > 80) {

                                // document.getElementById('btnV11').ImageURL = "~/Images/Valve/Valve-gray-left.png";
                                document.getElementById('btnV11').src = "../../Images/Valve/Valve-gray-left.png";           
                        });  
        }, 3000);
    });

and in <body>:

<asp:ImageButton ID="btnV11" runat="server" Height="51px" ImageUrl="~/Images/Valve/Valve-alarm-left.gif" Width="53px" OnClientClick="ShowControlPanel_P11(); return false;"/> 

Why do I get this error?


Solution

  • The id would be different than what you set in server side code. replace btnV11 with <%= btnV11.ClientID %>

    $(document).ready(function () {
               //doing other stuffs
            setInterval(function () {
    
                             $(".industrial.tank.size.one").each(function () {
                                $(this).industrial(t);
    
                                if (t > 80) {
    
                                    // document.getElementById('btnV11').ImageURL = "~/Images/Valve/Valve-gray-left.png";
                                    document.getElementById('<%= btnV11.ClientID %>').src = "../../Images/Valve/Valve-gray-left.png";           
                            });  
            }, 3000);
        });