Click here to Skip to main content
15,902,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to hide asp image control using java script
. I tried following code but its not working. image is not visible when on change function is called.

javascript is at client side if I set visibility property of image control to false then can i make it visible through javascript?

What I have tried:

image control code:

<asp:FileUpload ID="FileUpload2" runat="server" onchange="previewFile()" class="fa fa-camera" style="opacity:1;max-width:15px; max-height:11px; margin-top:4px"/> <%--onServerClick="postimagescrap_Click"--%>

javascript:


<script type="text/javascript">
function previewFile() {
var preview = document.querySelector('#<%=Image4.ClientID %>');
var file = document.querySelector('#<%=FileUpload2.ClientID %>').files[0];




document.getElementById("Image4").style.visibility = "visible"
var reader = new FileReader();

reader.onloadend = function () {
preview.src = reader.result;
}

if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
}
</script>
Posted
Updated 27-Aug-16 18:12pm
v2
Comments
ZurdoDev 27-Aug-16 21:32pm    
Debug your code. This is so simple and fast for you to fix on your own.

If you try to hide the Image asImage1.Visible = true;
in the server, the Image element will be completely removed from the DOM[^]. and you wont be able to control the visibility in javascript( client side).

try this, in c# to hide the element
C#
Image1.Style.Add("display","none");

now the visibility functionality will work as mentioned in the Solution 1

Note: display:none removes the element from the place, allowing other elements to fill in that place where as

visibility:hidden leaves the element in the declared place of the page such that it will still occupies the space
 
Share this answer
 
HTML DOM Style visibility Property[^] where element will still occupy space when invisible
JavaScript
document.getElementById("Image4").style.visibility = "visible";
document.getElementById("Image4").style.visibility = "hidden";


HTML DOM Style display Property[^] where element will not occupy space when hidden
JavaScript
document.getElementById("Image4").style.display = "block";
document.getElementById("Image4").style.display = "none";
 
Share this answer
 
Comments
Kishor-KW 27-Aug-16 20:32pm    
please see the code which i posted. I already tried this it's not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900