views:

96

answers:

1

Let me give a better description of exactly what I'm wanting to do here. I'm using Wikispaces, and by default, when a user adds a new page link to the side navigation bar, wikispaces inserts either one of two classes: wiki_link or wiki_link_new. Here's what it looks like...

<a href="/Map+Page" class="wiki_link">Map Page</a><br/>
<a href="/Staff+Olympics" class="wiki_link">Staff Olympics</a><br/>
<a href="/Staff+Meetings" class="wiki_link_new">Staff Meetings</a><br/>

I'd like to automatically remove both classes (wiki_link and wiki_link_new), and add a class to the link that better represents the page url. So, for /Map+Page url, a class would be added to the link that is the first 3 letters of the link title... map. And then, if the link url has two words (as in /Map+Page), the class would also add the first 3 letters of the second word in the url, preceded by a dash. So, for /Map+Page, the class to be added to this link would be

class="map-pag"

Or, if it was the /Staff+Olympics url, the class to be added would be...

class="sta-oly"

And I guess if there were more than 2 words within the a:link url, the excess words would be ignored in regards to applying a CSS class to the link, only paying attention to the first 3 letters of the first word, followed by a dash, and then followed by the first 3 letters of the second word.

And on top of all of that, I'd like to remove all instances of <br/>, which are also inserted by default by wikispaces.

So ideally, I'd like a jQuery solution that would change the 3 a:links above to this...

<a href="/Map+Page" class="map-pag">Map Page</a>
<a href="/Staff+Olympics" class="sta-oly">Staff Olympics</a>
<a href="/Staff+Meetings" class="sta-mee">Staff Meetings</a>

Not sure if my class naming conventions are the best. So if you have a better suggestion, I'd love to hear it! :)

Thanks for any help you can provide!

+2  A: 

This should do what you want:

// Select the links, and remove their classes
$(".wiki_link, .wiki_link_new").attr('class','').each(function(){
    var className = $(this).attr('href').substr(1).toLowerCase().split('+').slice(0,2).join('-');
    $(this).addClass(className);
});

I would just hide the <br /> tags with CSS. Manipulating the DOM is expensive in time and resources, so if you can just hide them it would be faster.

CSS

#container br { display: none; }

Where #container is whatever element is the parent of those a tags.

If you really want to use jQuery:

jQuery

$("#container br").remove();

Again, where #container is the parent of the list of a tags.

Doug Neiner
Hmmm... for some reason, the CSS trick to not display the <br/> tags is not working. I'll try with jQuery then. Going now to try your solution, Doug.
Spencer B.
I updated my answer to remove the Regular expression and replace it with better more obvious code.
Doug Neiner
@Spencer B. Its really important that `#container` is set to whatever element surrounds the `br` tags. Otherwise, it won't match anything. If that is correct, you could also try `#container br {display:none !important}` and see if that helps.
Doug Neiner
Hey Doug, thanks so much for helping me out on this. I just realized something though. With the problem you helped me out with the other day, I now have conflicting classes with a class being added to the html element that is based on the first word of the page url. So, the added CSS class for the links will always conflict with the CSS class added to the HTML element if the page url is only one word. Is it possible to create classes for the links based on my suggestion above?Your solution to removing the <br/>'s worked!
Spencer B.
Oh yeah... forgot about that, Doug. And I've been overriding current default wikispace styling using that same method, and here I am totally forgetting about that... ha ha. My bad...
Spencer B.
@Spencer I get what you are saying, but I am confused as to what you want to do now. In this answer, I thought I followed your guidelines exactly... is something different than you expected? Also, you could just scope your `CSS` like this: `a.map` and `html.map` vs the conflicting `.map` by itself. Sorry I didn't get what you were saying :(
Doug Neiner
Oh that's ok. I guess I was thinking that if the HTML element was receiving the same class name via jQuery, that there would be a conflict within the CSS that would prevent unique styling for different elements with the same class. But you're saying that if I simply specify the element before the same class name, that I can write different CSS styles for both of those elements, and they won't conflict? Does that make sense?
Spencer B.
Right, exactly! In your CSS scope the class to an element `a.map` would only apply to the classes added to that `a` element. Does that work for you?
Doug Neiner
Yes, that will work! Wow, thanks again Doug. I'm about to ask another question on here regarding removing come default embedded CSS that wikispaces inserts into table of contents. All this unnecessary CSS that wikispaces inserts is making me crazy. If they could just provide some CSS hooks for me, I'd be golden. :)
Spencer B.
Maybe I can save you a question, though I wouldn't mind the rep :) Just call `$('selector').removeAttr('style')` where selector equals the CSS selector like `#table-of-contents a` or `#toc *` etc.
Doug Neiner