tags:

views:

51

answers:

2

I have a bunch of urls that toggle user settings I would like to add some unobtrusive jQuery so the page doesn't have to reload every time. Everything I've found so far deals with forms, where as this is just a URL submit via js.

How do I add ajax url submit to the a attribute.

Ex:

<a class="user_attrs" href="/users/testuser/attr/img/1" alt="show image"/></a> 

I'd like to change all a.user_attrs links on the page so that they can post the url via ajax/jquery

thanks

+2  A: 

I think you may be going for something like this:

$('a.user_attrs').click(function(e) {
    var ev = e || event; 
    $.post($(this).attr('href'), function(data) {
       //do something with data...
    });
    ev.preventDefault();
});
Jacob Relkin
yup that is exactly it... seems the .preventDefault() is what I was missing.
Nick Faraday
A: 

Perhaps something like that?

$("a.user_attrs").click(function(evt){
    evt.preventDefault();
    $.post($(this).attr("href"),function(response){
        //Here you can add some feedback
    });
    return false;
});

I always do this way and works pretty well for me...

Minkiele
You don't need `return false` when you're calling `preventDefault` on the event object.
Jacob Relkin
Thanks for the advice
Minkiele