mrwhynot mrwhynot - 18 days ago 5
HTML Question

jQuery way to Replace character from a string

I have a generated navigation, The first n items have

_
in front of them to always be at the top of list. What I want to accomplish now is to remove that
_
from the front-end of the site.

I tried to do this with the jQuery code below which almost worked but it is also changing the text of other nav items.

See full mark-up on JS Fiddle: https://jsfiddle.net/1fz661o4/

Code:

$(function() {
$('.dropdown-submenu').html($('.dropdown-submenu').html().replace(/\_/g,''));
});

Answer

You could just change it to loop over the anchors.

$(function() {
    $('.dropdown-submenu > a').each(function() {
        $(this).text($(this).text().replace(/\_/g, ''));
    });
});

https://jsfiddle.net/utk12jdt/1/

Comments