0

How do you pass values from a link like this to a function <a href="search=sony&offset=20&lang=en" class="more_info">Read More</a> This link isn't static and values change depending on the search term and page number.

When a user clicks this link, it sends those 3 values to demo_search.php. Results are returned as html. Only a div on the page is refreshed.

I cant figure out how to get those values to the data part in the function. Can you please help me with that.

$(function() {

    $(".more_info").click(function() {
            // ajax call
            $.ajax({
                type: "GET",
                url: "demo_search.php",
                data: Need help in getting those values from the href here,
                beforeSend: function(html) {

                    $(".imgProgress").html(imgLoad);

                },
               success: function(page_data){

                    $("#results").empty();
                    $("#results").append(page_data);
                    $(".imgProgress").empty();
              },

            });    
        }
        return false;
    });
});
Reporter
  • 3,547
  • 5
  • 28
  • 45
Norman
  • 5,685
  • 19
  • 74
  • 128
  • 1
    you should be able to do: `data: this.href` since your href contains data that can be directly placed into the data option. – Kevin B Jul 23 '12 at 14:56
  • Possible duplicate to : http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript – DevlshOne Jul 23 '12 at 15:01

3 Answers3

3

try:

url: "demo_search.php?"+$(this).attr("href"),
mgraph
  • 14,736
  • 4
  • 36
  • 72
  • Personally I'd make the `href` attribute the full URL, rather than just the query string, then do `url: this.href`, but this way works too. – Anthony Grist Jul 23 '12 at 15:04
2

When calling $.ajax(), you can actually pass the data as a query string, so

data : this.href 

should work.

jay c.
  • 1,411
  • 10
  • 9
0

try this

$(function() {

    $(".more_info").click(function() {
            var targetLink = this;
            $.ajax({
                type: "GET",
                url: "demo_search.php?"+$(targetLink).attr("href"),
                beforeSend: function(html) {

                    $(".imgProgress").html(imgLoad);

                },
               success: function(page_data){

                    $("#results").empty();
                    $("#results").append(page_data);
                    $(".imgProgress").empty();
              },

            });    
        }
        return false;
    });
});​