Klikster
Klikster

Reputation: 1164

Trying to remove anchor text within brackets via jQuery / replace()

I've been searching through stackoverflow to find a solution to this but I can't seem to figure out what I'm doing wrong. I'm trying to select anything inside of "( )" brackets in primary/parent links within a menu and remove them with .replace() but nothing seems to be happening.

JSFiddle: http://jsfiddle.net/1rndwz3u/1/

JS:

jQuery(document).ready(function () { 
    jQuery(".menu-name-menu-category-menu ul.nav li > a").html().replace(/\(.*?\)/g,"");
});

HTML:

<div class="menu-name-menu-category-menu">
    <ul class="menu nav">
        <li class="first expanded dropdown active-trail">
            <a title="Heavy Equipment Rentals (0)" href="#">Heavy Equipment Rentals (0)<span class="caret"></span></a>
            <ul class="accordion-body">
                <li><a title="Excavator Rentals (2)" href="#">Excavator Rentals (2)</a></li>
                <li><a title="Mini Excavator Rentals (6)" href="#">Mini Excavator Rentals (6)</a></li>
                <li><a title="Skid Steer Rentals (9)" href="#">Skid Steer Rentals (9)</a></li>
            </ul>
        </li>
        <li class="active-trail"><a title="Tool Rentals (1)" href="#">Tool Rentals (1)</a></li>
        <li class="last expanded dropdown active-trail">
            <a title="Construction Rentals (0)" href="#">Construction Rentals (0) <span class="caret"></span></a>
            <ul class="accordion-body">
                <li><a title="Gator, UTV &amp; ATV Rentals (2)" href="#">Gator, UTV &amp; ATV Rentals (2)</a></li>
            </ul>
        </li>
    </ul>
</div>

I've tried using .html() and .text() but neither did anything, also tried grabbing just "Rentals" instead of a regex for the brackets and that didn't work either. Help is greatly appreciated. Thanks.

Upvotes: 0

Views: 402

Answers (3)

faby
faby

Reputation: 7558

working fiddle here

jQuery(document).ready(function () { 
    $( ".menu-name-menu-category-menu ul.nav li.expanded > a" ).each(function( index,elem ) {
          $(elem).html($(elem).html().replace(/ *\([^)]*\) */g, ""));
    });

});

Upvotes: 1

dehrg
dehrg

Reputation: 1741

You are editing the value but not doing anything with it. Try wrapping it with one more html() call:

jQuery(document).ready(function () {
    var target =  jQuery(".menu-name-menu-category-menu ul.nav li > a");
    target.html(target.html().replace(/\(.*?\)/g,""));
});

Upvotes: 2

jwatts1980
jwatts1980

Reputation: 7356

I haven't looked a the Fiddle yet but

jQuery(".menu-name-menu-category-menu ul.nav li > a").html().replace(/\(.*?\)/g,"");

returns a string and must be set back into the HTML.

var HTML = jQuery(".menu-name-menu-category-menu ul.nav li > a").html().replace(/\(.*?\)/g,"");
jQuery(".menu-name-menu-category-menu ul.nav li > a").html( HTML );

Upvotes: 2

Related Questions