Click here to Skip to main content
15,898,770 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently I am trying to call a javascript function in the VB Page_Load part. Currently I have got to respond through using alerts. My question is how can I change the visibility attribute to one of my buttons. When I try to:
JavaScript
var rawr = document.getElementById('OPENSEHMMSID');
rawr.setAttribute("visible", false);
alert(rawr.getAttribute("visible"))

My alert gives that it is set to false but I still see the button in my page. This javascript function is currently in the head. Anyone know how to make the attribute to work?
Posted
Comments
Sergey Alexandrovich Kryukov 20-Jun-12 14:30pm    
Why this attribute at all? This is not a best way, in most cases.
--SA
Member 8336910 20-Jun-12 15:04pm    
Well I have a button where it will become visible if there is a certain file on the client's computer. So in my javascript I have a way to see if the client has a file. Depending if they do or don't, I want to change the visibility of the button.

1 solution

I would advise that you prefer changing the style attribute instead of visibility, to make you code more universal and regular while gaining more fine-grain control. Also, I would advise you do it using jQuery.

Please see the jQuery methods .addClass(), .removeClass(), .toggleClass() and other CSS methods:
http://api.jquery.com/category/css/[^].

You can find a code sample here:
http://api.jquery.com/addClass/[^].

The CSS property you would need to operate would be visibility; please see:
http://www.w3schools.com/css/css_display_visibility.asp[^].

As a rule of thumb, if some effect can be achieved both through CSS and from more specialized attibutes, CSS approach should be preferred. It will help you to have more supportable code because extra flexibility and isolation between code and style will make possible changes less painful.

—SA
 
Share this answer
 
Comments
Member 8336910 20-Jun-12 15:00pm    
I figured out how to use the classes. Now I got to figure out how to make it work with javascript. Add the class through javascript.
Sergey Alexandrovich Kryukov 20-Jun-12 15:37pm    
If is not related to your original question. I gave you the exact recipe how to control visibility in JavaScript.
Are you accepting this answer formally (green button)?

As to the classes, JavaScript is not object-oriented. It uses inheritance, but via prototypes, not classes:
http://en.wikipedia.org/wiki/JavaScript#Prototype-based

The classes you have "figured out" might be about .NET. JavaScript code does not call .NET methods, .NET code does not call JavaScript functions, ever. This is not how ASP.NET works. Any questions on that?

--SA
Member 8336910 20-Jun-12 15:46pm    
Oh the class I was talking about was like the div.hidden{visibility:hidden}
Also if you use Page.ClientScript.RegisterStartupScript isn't that using a .NET method calling a javascript function? Well you did solve what I needed. It is just a rearrangement and ordering problem now. Thanks
Sergey Alexandrovich Kryukov 21-Jun-12 1:00am    
Ah... sorry that I did not get it; this term is rarely used compared to "class" as in OOP. Well, here the last paragraph of my answer apply. You see, historically, HTML did not have CSS, and many attributes and even elements were introduced to play the role of what later were put to CSS. For backward compatibility, almost all attributes were kept (some are subject of deprecation, but major part not). During development CSS should be strongly preferred though (not only in jQuery, JavaScript).

Good luck, call again.
--SA
Maciej Los 20-Jun-12 15:32pm    
Nice, 5!

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