views:

39

answers:

3

I have a master page which has a DIV with asp:Hyperlink controls functioning as the menu for the site.

When I'm on a certain page if (document.title = 'Certain Page'), I want javascript which will loop through all asp:hyperlink controls on the page and hide them (i.e. set to not visible). I know I could probably do this on the server side with less effort, but I'd rather do it via javascript.

+1  A: 

You will need to decorate your asp:Hyperlink's with a class that can be used to identify them on the client-side (you could also infer this from the control-id generation, but that would be more prone to errors, especially since ASP.NET 4.0 offers some alternatives).

One these links have a common/consistent class then you would just need to have the Javascript select items with that class and hide them. Depending on your choice of client-side libraries (or lack thereof) this could be something like a simple line of jQuery (assuming your asp:Hyperlinks have class="aspLink"): $('.aspLink').hide();

STW
what if i used a naming convention with my links, like linkMenu and linkProfile...is there any way I could match on asp control id name instead of using class?
Albert
you *could*, but it's a case where using a `class` is a better fit. Looping through elements and manually examining their `id` is likely to be much less performant than using classes (although some browsers will likely do just that).
STW
sure you can with jquery. For all hyperlinks (that will be rendered on <a>) ID terminated by linkMenu you can use $('a[id$linkMenu]').hide(); for links containing use $('a[id*linkMenu]').hide(); etc
FrenchiInLa
Sorry I made a typo I meant $('a[id$=linkMenu]').hide() and $('a[id*=linkMenu]').hide();
FrenchiInLa
@FrenchilnLa -- good point, provided jQuery is available (I know, I used it in my example too :P )
STW
+1  A: 

The only way to determine for sure what was an asp:hyperlink as opposed to a regular hyperlink <a> tag is going to be to add a class to all your asp:hyperlink tags. You could then use some jquery like

$('.AspLink').remove();

If you have some other way to determine that they are uniquely asp:hyperlink tags you can use some other jquery expression similarly. So if you have your <div> and know all links in the div are asp:hyperlink you can just use

$('#MyMenu a').remove();
jarrett
what if i used a naming convention with my links, like linkMenu and linkProfile...is there any way I could match on asp control id name instead of using class?
Albert
Potentially, maybe this will give you some ideas. Use this http://api.jquery.com/contains-selector/ for general search or this for your IDs http://api.jquery.com/attribute-contains-selector/
jarrett
+1  A: 

Assign an ID to the outer DIV, then use something like follows:

var childs = document.getElementById("outerDiv").childNodes;
for(var i=0; i<childs.length;i++) {
   if(childs[i].tagName == "a") childs[i].style.display = "none";
}

But actually you can do the same in code-behind of the master page.

Artem K.
this wouldn't scale to handle nested elements; you would need to at least do this recursively--and then, depending on the OP's exact scenario, this may hide `<a>`'s which were not generated from `asp:Hyperlink` controls.
STW
The benefit is the performance, I made it on the assumption of the given information: asp:Hyperlink inside the DIV.
Artem K.