3

I have done this functionality in a static way but how can i do this scroll using jquery so that it will smoothly scroll to the particular element which id i have added dynamically.

var data = [{"name": "Aruba", "code": "A"},{"name": "AndorrA", "code": "A"},{"name": "Bhutan", "code": "B"},{"name": "Bolivia", "code": "B"}]
var num=0;
$.each(data, function(key, val) {
    if (!$("#aZContent ul." + val.code).is("*")) {
     $("<ul />", {"class": val.code,"html": "<li>"+ val.name + "</li>"}).appendTo("#aZContent").before('<b class=' + val.code + ' id="letter_' + num++ + '">' + val.code + '</a></b>');
 } else {
  $("b." + val.code).each(function() {
      if (this.textContent === val.code) {
    $(this).next("ul").append("<li>" + val.name + "</li>");
   }
     });
 }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="alphabet" id="alphaTab">
 <a href="#letter_0">A</a>
 <a href="#letter_1">B</a> 
</div>  
<div id="aZContent">
 <ul></ul>
</div>
<div style="height:500px"></div>

On Click of link 'A' it should scroll to the "A" and likewise

  • you can replace all the else with `$("b." + val.code).next("ul").append("
  • " + val.name + "
  • ");` – caub Jun 21 '15 at 20:01
  • @crl i have done that scroll in a static way how can i do a smooth scrolling to the element using jquery – Manas Ranjan Pradhan Jun 22 '15 at 03:30
  • Static way? do you mean with #hash-id? So you want to replace that with your custom way to scroll? Can I ask why? – caub Jun 22 '15 at 10:26
  • @crl when i am trying to implement this prototype in actual Application it will not work as i am using prevent default. Thats why i need to do it in a custom way to scroll using jquery. – Manas Ranjan Pradhan Jun 22 '15 at 16:31