Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add individual text replacing each number for JavaScript menu

I want to replace the numbers in the HTML menu li link class"scrollTo" (created by JavaScript) to individual text rather than the 01, 02, 03, 04 numbers

function createSliderPagination(container){
      var wrapper = $('<ol class="slot-navigation"></ol>');
      container.children('.slot-slider').find('li').each(function(index){
        var dotWrapper = (index == 0) ? $('<li class="selected"></li>') : $('<li></li>'),
            dot = $('<a href="#0"></a>').appendTo(dotWrapper);
        dotWrapper.appendTo(wrapper);
        var dotText = (index+1 < 10) ? '0' + (index+1): index+1;
        dot.text(dotText);
      });
    wrapper.appendTo(container);
    return wrapper.children('li');
  }

Html code created is this that the JavaScript is creating

<ol class="slot-navigation">
<li><a href="#0" class="scrollTo">01</a></li>
<li class="selected"><a href="#0" class="scrollTo">02</a></li>
<li><a href="#0" class="scrollTo">03</a></li>
<li><a href="#0" class="scrollTo">04</a></li>
<li><a href="#0" class="scrollTo">05</a></li>
</ol>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use a switch statement to evaluate index :

function createSliderPagination(container) {
    var wrapper = $('<ol class="slot-navigation"></ol>');
    container.children('.slot-slider').find('li').each(function(index) {
        var dotWrapper = (index == 0) ? $('<li class="selected"></li>') : $('<li></li>'),
            dot = $('<a href="#0"></a>').appendTo(dotWrapper);
        dotWrapper.appendTo(wrapper);
        var dotText;
        switch(index) {
             case 0:
                 dotText = "Home";
                 break;
             case 1 :
                 dotText = "Our services";
                 break;
             case 2 :
                 dotText = "Third item";
                 break;
             case 3 :
                 dotText = "Fourth item";
                 break;
             default :
                 dotText = "Default";
                 break;
        }
        dot.text(dotText);
    });
    wrapper.appendTo(container);
    return wrapper.children('li');
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading