Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, I Have a java script function but I unable to call it on button click,

Here is my Code

Script
<script type="text/javascript">
              function toggleVisibility() 
              {
                  var control = document.getElementById("FileUpload1");
                  
                  if (control.style.visibility == "visible" || control.style.visibility == "")
                      control.style.visibility = "hidden";
                  else
                      control.style.visibility = "visible";
                  return;
              }
      </script>


And Asp.Net Code as...

ASP.NET
<asp:Button ID="ChangePic" runat="server" Text="Change Pic" OnClientClick="toggleVisibility()" />
                       <asp:FileUpload ID="FileUpload1" Visible="false" runat="server" />


What I have tried:

I trying to controls visibility true or false using java script.
I use java script because i want to run my code at client side without posting back on server..
Posted
Updated 18-Dec-16 18:47pm

Hi Vikas, use below code. hope it'll helpful for you.
JavaScript
function myFunction() {
    var x = document.getElementById('IdofParticularControl');
    if (x.style.visibility === 'hidden') {
        x.style.visibility = 'visible';
    } else {
        x.style.visibility = 'hidden';
    }
}


<button onclick="myFunction()">Click me</button>
 
Share this answer
 
v3
Comments
Vikas Hire 7-Dec-16 3:18am    
Not Work.. :(
Shambhoo kumar 7-Dec-16 3:33am    
check solution again. This code is correct, you are not able to call it in correct way.
Vikas Hire 7-Dec-16 3:34am    
OK
Vikas Hire 7-Dec-16 3:35am    
can you tell me Where should correct place to place java script in my code
Shambhoo kumar 7-Dec-16 5:05am    
better to write this code in separate file.
Hello vikas you can call it by jquery to
$("#ChangePic").click(function(){

var control = document.getElementById("FileUpload1");

if (control.style.visibility == "visible" || control.style.visibility == "")
control.style.visibility = "hidden";
else
control.style.visibility = "visible";
return;
});

no no need to write this below code

OnClientClick="toggleVisibility()"
 
Share this answer
 
v2
 
Share this answer
 
Comments
Vikas Hire 7-Dec-16 3:22am    
I get changes according to provided link Context.. but still my code doesn't work
$*Developer - Vaibhav*$ 7-Dec-16 3:59am    
share your code (full page) or just write alert() for testing purpose

function toggleVisibility()
{
alert('Hi');
}
$*Developer - Vaibhav*$ 7-Dec-16 4:53am    
Please check, you have not put script tag


<script type="text/javascript" language="javascript">

function myFunction() {
var x = document.getElementById('IdofParticularControl');
if (x.style.visibility === 'hidden') {
x.style.visibility = 'visible';
} else {
x.style.visibility = 'hidden';
}
}
Launch the page on your browser, right click and view the page source , can you find the FileUpload1 control?
1. Since you specified Visible="false" for the FileUpload control, it will never be rendered on your HTML page at all, so you will never be able to access it through you client-side script. If you want to hide it upon page load and be able to toggle it via script, do not use the Visible="false" property, instead you have to do it on the client side by either setting the CSS property of display to none, or use JavaScript to hide it. e.g.
var control = document.getElementById("FileUpload1");
control.style.visibility = "hidden"; // to hide
// or
control.style.display = "none"; // to hide
// to show
control.style.display = "";

2. Having set it right after 1, launch it and view its page source again, can you find the id="FileUpload1", may be not, instead you may see something that looks like
id="MainContent_FileUpload1"
. Yes, the name may be prefixed with some other string to make it unique, to get the actual id, you have to use:
var control = document.getElementById("<%= FileUpload1.ClientID %>");

3. Lastly, all your controls will be inside a form, and the default behavior of any form submit is to sent the data to the server. You JavaScript code will toggle the control on each button click, but it will always be refreshed to the default hidden state as a result of the form submission. The solution is to cancel this default bahavior. Solution? Set it to return false in your JavaScript function, and add a return to the onClientClick attribute.
To wrap it up, the finished code will look like this:

<div>
    <asp:Button ID="ChangePic" runat="server" Text="Change Pic" onClientClick="return toggleVisibility()" />
    <asp:FileUpload ID="FileUpload1" runat="server" />
</div>

<script>
    var control = document.getElementById("<%= FileUpload1.ClientID %>");
    control.style.visibility = "hidden";
    function toggleVisibility()
    {
        if (control.style.visibility == "visible")
            control.style.visibility = "hidden";
        else
            control.style.visibility = "visible";
        return false;
    }
</script>
 
Share this answer
 
v2
try jquery instead

$("#ChangePic").click(function(){
toggleVisibility();
});
 
Share this answer
 

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