Florian Leitgeb
Florian Leitgeb

Reputation: 16624

Prevent button event from being fired

I know there is the Sharepoint portal, but in my opinion, this question is more related to javascript and cross browsing than to Sharepoint.

I have a custom ribbon button in Sharepoint which should start a workflow on a specific element in my library. I want to add a javascript/jquery check to verify some data and only if everything is fine, the button event should fire up.

<a unselectable="on" href="javascript:;" onclick="return false;" class="ms-cui-ctl-large " mscui:controltype="Button" role="button" id="AddNewButton-Large"><span unselectable="on" class="ms-cui-ctl-largeIconContainer"><span unselectable="on" class=" ms-cui-img-32by32 ms-cui-img-cont-float"><img unselectable="on" alt="Neue Testmappe erstellen" src="http://contoso:43265/sites/contoso/_layouts/Images/docset_captureversion_32.png" style="left: 0px;"></span></span><span unselectable="on" class="ms-cui-ctl-largelabel">Create Element<br>erstellen</span></a>

Tried everything, but the event is always firing. Also tried .unbind() or .off() methods and the .preventDefault(). Really dont know how to prevent the event from being fired.

Are there any special cross browser events that I should be aware of when trying to prevent from firing the event?

I can see the events in developer tools on my "a" element. And when I remove the events, everything works (or not). But it is impossible to do this programmatically.

Upvotes: 0

Views: 260

Answers (2)

margarita
margarita

Reputation: 894

In your onclick attribute, use a function:

function(event){ event.preventDefault(); }

As an alternative, you can write some javascript that will stop it:

with JQuery:

$("a").on("click", function(event){
    event.preventDefault();
    //any function that you need ...
});

Upvotes: 1

Ricardo van Laarhoven
Ricardo van Laarhoven

Reputation: 788

Use jQuery's preventdefault

If this method is called, the default action of the event will not be triggered.

$( "a" ).click(function( event ) {
  event.preventDefault();
  //and now do stuff like for example:
  $('body').hide();

});

Upvotes: 2

Related Questions