views:

92

answers:

1

I am using jquery to scroll to an anchor.... It works fine with a html button calling the function...

But when i call it from server side i get the id but it doesn't seem to work for me...

LinkButton lb1 = (LinkButton)sender;
ScriptManager.RegisterClientScriptBlock(lb1, typeof(LinkButton),
 "scroll","goToByScroll('myAnchor')", true);

And my function is,

<script type="text/javascript">
 function goToByScroll(id) {alert(id);
   $('html,body').animate({ scrollTop: $("#" + id).offset().top }, 'slow');
   }
 </script>

I got the error $("#" + id).offset() is null

I get the alert id of my anchor but why doesn't it work....

Here is my anchor...

<a name="myAnchor" id="myAnchor"></a>

Its working perfectly in a html button,

<input id="Button1" type="button" value="button" onclick="goToByScroll('myAnchor');"/>

A: 

You could try bringing your function to get scrolltop out of that area--not sure animate() can handle it.

var topOff = $("#" + id).offset().top;
$('html,body').animate({ scrollTop: topOff}, 'slow');

And if that doesn't do it, I dunno, I seem to remember having to convince jQuery I meant to make things a string in the selector, so $("#"+id+"").

D_N