This project has moved. For the latest updates, please go here.

Disabling page links

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

I tried this:

$(" a").click(function() {
I also tried using the preventDefault method, removing the href attribute, adding return false and a lot of other methods.

Please help.
Jan 17, 2014 at 2: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?

Jan 17, 2014 at 2:47 PM
unwrap() actually removes the link altogether (the parent of the "a" tag is a a "td").
Jan 17, 2014 at 2: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.

Jan 17, 2014 at 3: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.
Jan 17, 2014 at 3: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:
$(" a").click(function(e) {
  // Do what you want here
Jan 17, 2014 at 4:00 PM
Tried that, but it doesn't work for me.
Jan 17, 2014 at 5:22 PM
Edited Jan 17, 2014 at 6: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.

jQuery(document).ready(function($) {
    var $links = $(" a");
    $links.each(function() {
        var $this = $(this)

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

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

        // Do what you want here        
Jan 17, 2014 at 5:49 PM
Thanks a lot. That worked!
Feb 27, 2014 at 1: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?
Feb 27, 2014 at 2: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.

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