Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How to use e.preventDefault(); in a JavaScript function

I have this JavaScript function :

function putVote(trackid, vote) {
}

and I call this function trought :

<a href="#" onClick="putVote('data1', 'data2')">Link</a>

I would like to use e.preventDefault(); on putVote(), but I think I'm wrong in some ways. How can I do it?

Cheers

like image 266
markzzz Avatar asked Dec 04 '22 09:12

markzzz


1 Answers

The simplest thing to do would be to return false from the function in the handler (return false would only work in putVote if the handler had return putVote('data1', 'data2)).

But as Pointy said, a much better technique is to attach the event handler from JavaScript, most easily achieved by using a library/framework such as jQuery or Prototype.

like image 165
Skilldrick Avatar answered Apr 02 '23 02:04

Skilldrick