0

I've got a function that scrolls to an anchor:

function scrollToAnchor(aid) {
    var aTag = $("a[name='" + aid + "']");
    if (aTag.length) {
        $('html,body').animate({
            scrollTop: aTag.offset().top - 100
        }, 'slow');
        aTag.closest('.subpanel').effect("highlight", 5000);
    }
}

HTML

<a id="A2" class="gridLabel" name="Add Action Item">
    <span id="MainContent_Label19" title="Add / Edit an action item.">Add / Edit Action Item</span>
</a>

I have to call a server side event that performs some sort of action. Once the action is completed I need to call this scrollToAnchor. I tried this:

ScriptManager.RegisterClientScriptBlock(this, GetType(), "OpenActions", "$(function(){ 
 function scrollToAnchor(aid) { var aTag = $('a[name=''' + aid + ''']');if (aTag.length) 
{$('html,body').animate({ scrollTop: aTag.offset().top - 100 }, 
'slow');aTag.closest('.subpanel').effect('highlight', 5000);}} $('#tblAction').show(); 
scrollToAnchor('Add Action Item');});", true);

However I get errors in the console probably because of my ' ' and " ". Can someone help form this for me.

I also tried:

ScriptManager.RegisterClientScriptBlock(this, GetType(), "OpenActions", "$(function()
{function scrollToAnchor(aid) { var aTag = $('a[name=\"' + aid + '\"]' + ']');if 
(aTag.length) {$('html,body').animate({ scrollTop: aTag.offset().top - 100 }, 
'slow');aTag.closest('.subpanel').effect('highlight', 5000);}} $('#tblAction').show(); 
scrollToAnchor('Add Action Item');});", true);
Xotic750
  • 21,928
  • 8
  • 53
  • 76
JonH
  • 31,954
  • 11
  • 81
  • 141

3 Answers3

1

The issue seems to be here:

'a[name=''' + aid + ''']'

Try replacing that with:

'a[name=' + aid + ']'

Or if you need quotes in the value of name:

'a[name=\'' + aid + '\']'
// Or
'a[name=\"' + aid + '\"]'
Cerbrus
  • 65,559
  • 18
  • 128
  • 140
  • This is almost working but here's the error i get: Uncaught Error: Syntax error, unrecognized expression: a[name=Add Action Item]] – JonH Feb 11 '14 at 14:21
  • @JonH: Then you'll need to use one of the last 2 examples. – Cerbrus Feb 11 '14 at 14:23
  • Now Im getting Uncaught SyntaxError: Unexpected string with the first example of the third example you posted. Here is my code: `ScriptManager.RegisterClientScriptBlock(this, GetType(), "OpenActions", "$(function(){function scrollToAnchor(aid) { var aTag = $('a[name=\'' + aid + '\']' + ']');if (aTag.length) {$('html,body').animate({ scrollTop: aTag.offset().top - 100 }, 'slow');aTag.closest('.subpanel').effect('highlight', 5000);}} $('#tblAction').show(); scrollToAnchor('Add Action Item');});", true);` – JonH Feb 11 '14 at 14:27
  • And if I use the last example I get this `Uncaught Error: Syntax error, unrecognized expression: a[name="Add Action Item"]] ` – JonH Feb 11 '14 at 14:29
  • 1
    @JonH You have 2 closing brackets now: [...Item"]] – fast Feb 11 '14 at 15:15
1

What I'd do is put the function on the page (or in a js file loaded by the page) and then simply register a script like so:

    ScriptManager.RegisterClientScriptBlock(this, GetType(), 
"OpenActions", "scrollToAnchor('Add Action Item');", true);

From what I can tell, you don't need to register the entire script on each server side event. You just need to run the function with a given argument.

mikey
  • 4,968
  • 3
  • 22
  • 27
-1

Prefix your string with @, like so

 ScriptManager.RegisterClientScriptBlock(this, GetType(), "OpenActions", @"$(function(){ 
 function scrollToAnchor(aid) { var aTag = $('a[name=' + aid + ']');if (aTag.length) 
{$('html,body').animate({ scrollTop: aTag.offset().top - 100 }, 
'slow');aTag.closest('.subpanel').effect('highlight', 5000);}} $('#tblAction').show(); 
scrollToAnchor('Add Action Item');});", true);
spassvogel
  • 3,399
  • 2
  • 17
  • 22