1

YES, there is a link "How to go on a specific element on a page" which i already followed, so before you mark it as a duplicate, read this.

I tested every bit of the code given on the link and none of them worked.

I have this search field which gives an user an option to search. If search button is clicked the element should be scrolled to. I logged in the console the element, it is retrieved and shown in console, however any of the given functions in the link does not work. By the way, my bootstrap panel is filled with accordions, so maybe this is why the problem exists, however it should send me to the accordion atleast.

//Search button click
$("#searchbutton").click(function() {
    var searchInfo = document.getElementById("search");
    console.log(searchInfo.value);
    var playerNamer = findPlayerBy(playerLst,searchInfo.value,null);
    if (playerNamer == false){
        //Do nothing, if search fails
    }
    else{
        console.log(document.getElementById("id_"+playerNamer.pos));
        window.scroll(0,findPos(document.getElementById("id_"+playerNamer.pos)));
    }
});

function findPos(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    return [curtop];
    }
}

Now as you can see on searchbutton click i have few console.log-s. These things return the right result, however one of the example functions from the page left in the code is not working.

I present my console output as an image cause copying in Opera console didn't work quite as expected:

IMAGE OF CONSOLE

The example may be a bit misleading though, because i used the top element. I have an element in the bottom which can't be seen on the screen and there isn't the scroll happening either (element with id of 8).

Community
  • 1
  • 1
Banana
  • 814
  • 1
  • 8
  • 28

2 Answers2

2

Change scroll to scrollTop:

$(window).scrollTop(findPos(document.getElementById("id_"+playerNamer.pos)));
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Uncaught typeerror, window.scrollTop is not a function. Seems like my jQuery is outdated.. – Banana Sep 21 '15 at 13:38
  • @Banana Try this: `$(window).scrollTop(findPos(document.getElementById("id_"+playerNamer.pos)));` – Praveen Kumar Purushothaman Sep 21 '15 at 13:41
  • Now it lost the error although no action happens on the screen. – Banana Sep 21 '15 at 13:44
  • 1
    Haven't used these features before, here's bin. Might be in trouble tho, cause i'm using autocomplete on jQuery UI and didn't know how to add this. http://jsbin.com/lexusisimu/edit this is bootply: http://www.bootply.com/fjZr88RY9z – Banana Sep 21 '15 at 13:55
0

I fixed it linking to the parent element (currently i had the id of child element). This probably caused all the panic and now i've got it to work.

Banana
  • 814
  • 1
  • 8
  • 28