dvancouver
dvancouver

Reputation: 723

JQuery Passing Variable From href to load()

I am trying to pass an href id to load() in JQuery, I can see from the alert the returned id# 960, so I know the id value is getting past I just don't know how to append the load url, well the $("#refreshme_"+add_id) is the important part, I use that to refresh the unique div id in the page assigned by the database pull, so it would be looking for id="refreshme_960". My simple example that is obviously not working. I want to refresh the db select part of the page showing the new list of cars. Any ideas?

$(document).ready(function() 
{
    $(".add_to_favorites").livequery("click", function(event) 
    {
        var add_id = this.id.replace("add_", ""); //taken from a.href tag
        alert (id);
        $.get(this.href, function(data) 
        {
            $("#refreshme_"+add_id).load("http://www.example.com/car_list/"+add_id);

<a class="add_to_cars" href="/car-add/960/add'}" id="add_960">Add Car</a>

Upvotes: 2

Views: 14510

Answers (2)

Mohamed Ali
Mohamed Ali

Reputation: 561

You are using append() in this line

$('#favorite-listings').append(response);

Which is why you get new results added after the old ones. Change that line to

$('#favorite-listings').html(response);

Upvotes: 1

pedalpete
pedalpete

Reputation: 21536

This is a bit confusing, are you have 'add_to_favorites' as your trigger, and then add_to_cars in the html element.

too keep your replace id all jqueryish, i think you would use

$(this).attr('id', 'add_');

I think what you are trying to do is that when a user clicks the 'add_to_cars' link, that it adds that from an ajax response into the favorites? Though i could be wrong.

I would question why use add_960 as your id instead of just 960 (I know you aren't supposed to use numbers as id's by I do it all the time and haven't had an issue yet).

So you could do

$('.add_to_cars')livequery('click', function(event){
     var request=$(this).attr('id');
  $.ajax({
           type: GET,
           url: "http://www.example.com/car_list/"+request,
           success: function(response){
                $('.add_to_favorites').append(response)


      }
});
});

or something like that. again, not entirely sure what you are trying to do.

Upvotes: 0

Related Questions