-1

i want to make my scroll go to the right div in a container, but every answer i tried failed. Here is my list

<div id="odjContent">
    <div id="1" class="rang0 obj" onclick="aller(0);" style="position:relative;">CCCC</div>
    <div id="4" class="rang0 obj" onclick="aller(0);" style="position:relative;">FFFFFF</div>
    <div id="2" class="rang1 obj" onclick="aller(0);" style="position:relative;">BBDDDDDDB</div>
    <div id="3" class="rang0 obj" onclick="aller(0);" style="position:relative;">EEEEEE</div>
    <div id="6" class="rang0 obj" onclick="aller(0);" style="position:relative;">FFFFFF</div>
    <div id="5" class="rang0 obj" onclick="aller(0);" style="position:relative;">EEEEEE</div>
    <div id="11" class="rang0 obj" onclick="aller(0);" style="position:relative;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam id aliquam leo, quis molestie mauris. Suspendisse id condimentum elit. Sed in quam porttitor, condimentum elit sit amet, condimentum nibh. In volutpat dolor sed mi pharetra, at cursus urna semper. Duis orci purus, porttitor eget metus vel, blandit pellentesque sapien. Nam auctor nunc vel fermentum convallis. Duis ut volutpat mi. Integer commodo euismod justo. Nullam venenatis tristique bibendum. Aliquam adipiscing eget lectus nec molestie. Fusce risus magna, dignissim ut mauris at, vehicula feugiat eros. Sed tincidunt egestas leo vel volutpat. Ut imperdiet, risus ac tempus ultricies, nisl nunc faucibus massa, a interdum dui lacus id magna.</div>

here is my js :

function gererFocus(id) {
    id = "#" + id;
    console.log("Id: " + id + "  " + $(id).offset().top);
    $('#odjContent').animate({
        scrollTop: $(id).offset().top
    });
}

It doesn't focus where i want and when i click twice or throw this method with the same arguments, i haven't the same result in

console.log("Id: "+id+"  "+$(id).offset().top);
  • here is how i call my div with an id : `` – user3005460 Nov 18 '13 at 16:27
  • what is aller? id's which are just numbers are not good. – caramba Nov 18 '13 at 16:31
  • thank you caramba but when i call this function i dont go throught aller i call it with : Thank you for your answer but i called the function with this code : – user3005460 Nov 18 '13 at 16:40
  • there is not id "pp", at least not it the postet code. if you use jquery you could only write $('#nameOfYourId').val(); – caramba Nov 18 '13 at 16:48
  • ok this was pp but i will try with jquery to get the value EDIT It doesn't solve my problem, focus still seem to be random – user3005460 Nov 18 '13 at 16:50
  • don't know what you want to do. get the id or scroll to an element? created a little fiddle, maybe this helps you if you try to paly a bit with this: http://jsfiddle.net/ppenk/ – caramba Nov 18 '13 at 16:59
  • i want to scroll to a div which id passed with this input : – user3005460 Nov 18 '13 at 17:12
  • in the sample you give me i want that when i click on one element the div which contain all this elements scroll to make the target div at the top of the box – user3005460 Nov 18 '13 at 17:16
  • fore me your question is very confusing, what should aller() be for a function? if you have and input type="button" show it. please edit your question and show all the code which is important to this question – caramba Nov 19 '13 at 16:33

1 Answers1

0

You need to do element.id as you are passing whole object in form of this so you need to catch that in your js function with element where element is your div that is clicked.

function gererFocus(element) {
    var id = "#" + element.id; // Change here
    .... rest of code .....
}

Hope this'll help!

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
  • Thank you for your answer but i called the function with this code : So i think i haven't to change this code var id = "#" + element.id; – user3005460 Nov 18 '13 at 16:40