Results 1 to 5 of 5

Thread: remove onclick from a href tag and add to jquery onclick code

  1. #1
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default remove onclick from a href tag and add to jquery onclick code

    hi all

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

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

    Code:
    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

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

    Code:
    $('.wishlink').click(function(){
    
    });
    vineet

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    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
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Beverleyh View Post
    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

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There are lots of ways this can be done. I like the idea of a link that sort of shows what it will do:

    Code:
    <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:

    Code:
    $('.wishlink').click(function(e){
    	getWISHID(this.hash.replace('#', ''));
    	e.preventDefault()
    });
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks john

    got it working now

    vineet

Similar Threads

  1. dynamically adding onclick to <a href>
    By gwest in forum JavaScript
    Replies: 6
    Last Post: 05-27-2012, 06:33 PM
  2. Image Thumbnail Viewer II custom href when use function onclick
    By saiborg333 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 06-11-2011, 12:12 PM
  3. Combine href w/ onclick for iframe lightbox
    By visibledentist in forum JavaScript
    Replies: 10
    Last Post: 05-22-2010, 01:27 AM
  4. Animated Collapse use onclick instead of href
    By admijn in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 04-03-2009, 12:31 PM
  5. image --> onclick --> href to target
    By Francisco Dias in forum JavaScript
    Replies: 1
    Last Post: 12-04-2007, 05:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •