1

I have a simple menu with this structure:

<ul class="navigation">
  <li><a href="javascript:void(0)" onclick="javascript:dataTable()">Preview all record</a></li>
</ul>

In some cases in my project I need to disable calling dataTable() function when user clicking on this tag

I am using this

$('ul.navigation a').click(function(event){
     event.preventDefault();
});

but it doesn't work with me

3 Answers 3

3

Try removing the onclick HTML attributes instead:

$("ul.navigation a").removeAttr("onclick");
Sign up to request clarification or add additional context in comments.

2 Comments

the whole site uses ajax to call all pages, so when i remove the onclick, how could i make it work again
Well, if all the links call dataTable(), you can call it explicitly. If you want to support arbitrary Javascript statements, though, you would have to persist the previous value of onclick somewhere else (maybe something like a data-old-onclick attribute) and invoke eval() on it from your event handler.
0

Try save the value and set the onclick attribute with nothing:

var tmpVal = $("ul.navigation a").attr("onclick");
 $("ul.navigation a").attr("onclick","");

And you can restore the value again :

$("ul.navigation a").attr("onclick",tmpVal );

Comments

0

You can use a variable to store whether to call dataTable() or not:

onclick="javascript:if(showTable)dataTable()"

http://jsfiddle.net/shZFj/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.