PDA

View Full Version : remove onclick from a href tag and add to jquery onclick code



vinpkl
09-01-2016, 03:30 PM
hi all

how to remove onclick from < a href > and add to jquery on click function



<a href="javascript:void(0);" onClick="getWISHID(5)" class='wishlink'>add to wishlist</a>





function getWISHID(id)
{
$.ajax({
type: "POST",
url: "wishlist.php",
data: "wishid="+id,


});

}



i want it to convert like below and pass the id

i think i need to pass the id reference in <a> tag also.

we can use data attribute or any other alternative



<a class='wishlink'>add to wishlist</a>





$('.wishlink').click(function(){

});


vineet

Beverleyh
09-01-2016, 03:59 PM
Try preventDefault() https://api.jquery.com/event.preventdefault/, although if you don't intend to send the user to another location, or access a resource, you shouldn't use an anchor element. Change it to a button instead. https://davidwalsh.name/html5-buttons

vinpkl
09-01-2016, 04:18 PM
Try preventDefault() https://api.jquery.com/event.preventdefault/, although if you don't intend to send the user to another location, or access a resource, you shouldn't use an anchor element. Change it to a button instead. https://davidwalsh.name/html5-buttons

thanks beverleyh

for the suggestions

i will update my code with them

vineet

jscheuer1
09-01-2016, 06:28 PM
There are lots of ways this can be done. I like the idea of a link that sort of shows what it will do:


<a class="wishlink" href="wishid#5">add to wishlist</a>

Using that type of link, this sort of jQuery click event will work with it and the existing getWISHID function:


$('.wishlink').click(function(e){
getWISHID(this.hash.replace('#', ''));
e.preventDefault()
});

vinpkl
09-02-2016, 02:53 AM
thanks john

got it working now

vineet