Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  83] [ 4]  / answers: 1 / hits: 36892  / 13 Years ago, sat, february 18, 2012, 12:00:00

Is there a simple way to get the parameters at the end of an href attribute of a clicked link using the click event object?



I have some jQuery code that looks like this:



$(document).ready(function() {
$('#pages').delegate(a, click, function(e) {
var formData = parameters to clicked link;
$.ajax({
url: 'friends2.php',
dataType: 'json',
data: formData,
success: function(data) {
$('#searchbutton').attr(disabled, false);
$('#searchresults').html(data.results);
$('#pages').html(data.paginate);
}
});//ajax end
return false;
});
});


And here is the relevant HTML:



<div id=pages>
<span class=disabled>previous</span>
<span class=current>1</span>
<a href=friends.php?term=ma&p=2>2</a>
</div>


What I'm trying to do is paginate the results of a search that I've done with ajax. The search runs fine and a get a list of users that match the query. The part of the results script that creates the pagination links is also working.



In the example above, there are two pages of results. What I want to do when a user clicks the link to go to page 2 of the results is to intercept the link click and instead create a new ajax request using term=ma&p=2 as the data passed to the request.



So, long story short, is there an easy way to get term=ma&p=2 from the event object passed the the anonymous function in my jQuery above?


More From » jquery

 Answers
6

You can use the this.href method to read the link attribute:



$('#pages').delegate(a, click, function(e) {
var str = this.href.split('?')[1];


Example:



str = 'friends.php?term=ma&p=2';
console.log(str.split('?')[1]); // output: term=ma&p=2

[#87375] Friday, February 17, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
longd

Total Points: 616
Total Questions: 110
Total Answers: 101

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;