Disabling page links

Jan 17, 2014 at 3:12 PM
Edited Jan 17, 2014 at 3:15 PM
I'm trying to disable page links on a SharePoint page using jQuery.

I tried this:

$("td.ms-vb a").click(function() {
       $(this).removeAttr("onclick");
});
I also tried using the preventDefault method, removing the href attribute, adding return false and a lot of other methods.

Please help.
Coordinator
Jan 17, 2014 at 3:32 PM
This question doesn't have anything to do with SPServices, but what the heck.

Have you tried using .unwrap() on all of the links?

M.
Jan 17, 2014 at 3:47 PM
unwrap() actually removes the link altogether (the parent of the "a" tag is a a "td").
Coordinator
Jan 17, 2014 at 3:50 PM
You'd want to unwrap the a tag from the text it contains. It depends on what your real goal is, of course.

M.
Jan 17, 2014 at 4:26 PM
Sorry, I know this is not exactly related to SPServices. Here's what what I want to do.

I have page with lookup column links. When users click on the link, instead of going to a different page (a DispForm.aspx), I want to display the list items matching that particular ID under the clicked link. This is so I can create an accordion effect. After the list items display, the link target page appears. I don't want that to happen.
Coordinator
Jan 17, 2014 at 4:33 PM
OK, so you don't actually want to remove al the links in the page; you want to change what happens when the user clicks on some of them.

Something like this should work:
$("td.ms-vb a").click(function(e) {
  e.preventDefault();
  // Do what you want here
});
M.
Jan 17, 2014 at 5:00 PM
Tried that, but it doesn't work for me.
Jan 17, 2014 at 6:22 PM
Edited Jan 17, 2014 at 7:15 PM
The onclick is firing even though you are removing it from the code. Once you click it, it fires, then you remove the onclick function. What I would suggest is to cache and remove all of your onclick's upon $(document).ready. Once those are cached and removed, then you can use the code Marc provided above.

Example:
jQuery(document).ready(function($) {
    var $links = $("td.ms-vb a");
    
    $links.each(function() {
        var $this = $(this)
        
        ;

        // Cache onclick handler.
        // Remove the default onclick.      
        $this.data('onclick', this.onclick);
        // $this.data("onmousedown", this.onmousedown);
        // $this.data("onfocus", this.onfocus);

        
        this.onclick = $.noop;
        // this.onmousedown = $.noop;
        // this.onfocus = $.noop;
    });
    
    $links.on("click", function(event) {

        event.preventDefault();
        // Do what you want here        
    }); 
});
Cheers,
Matthew
Jan 17, 2014 at 6:49 PM
Thanks a lot. That worked!
Feb 27, 2014 at 2:46 PM
This works really well except when I use Preview-style view. In that view, links on the left pane are disabled, but those on the right pane (the preview) do not. How can I disable those as well?
Coordinator
Feb 27, 2014 at 3:15 PM
It's probably a timing issue. Depending on what type of page it is, the content in the right pane may be loading asynchronously.

M.
Feb 27, 2014 at 3:29 PM
The right pane's content loads on mouseover. How do I disable those links?
Coordinator
Feb 27, 2014 at 3:30 PM
Edited Feb 27, 2014 at 3:30 PM
Really hard to say. Perhaps on the mouseover you can run the "delinkification".

M.