1

The following code works, but I'm sure there is a more compact way to achieve the same result, particularly the string replacement:

$('#nav-main .nav-sub').each(function() {

    var name = $(this).attr('id');

    $(this).children('li').children('a').each(function() {

        var text = $(this).text().toLowerCase();
        var spaces = / /g;
        var sub = /sub-/;
        var id = name + '-' + text.replace(spaces, '-');    
        var id = id.replace(sub, '');       
        $(this).attr('id', id);

    });

});

1 Answer 1

1
$('#nav-main .nav-sub').each(function() {
    var name = $(this).attr('id');
    $('li a', this).each(function() {       
        this.id = (name + '-' + $(this).text().toLowerCase().replace(/\s/g, '-')).replace(/sub-/, '');
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.