Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the parameters of an href attribute of a link from the click event object

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?

like image 485
itsmequinn Avatar asked Feb 18 '12 17:02

itsmequinn


People also ask

How do I select an element using href?

If you want to get any element that has part of a URL in their href attribute you could use: $( 'a[href*="google.com"]' ); This will select all elements with a href that contains google.com, for example: http://google.com.

What are the attributes of href tag?

For <a> and <area> elements, the href attribute specifies the URL of the page the link goes to. For <base> elements, the href attribute specifies the base URL for all relative URLs on a page. For <link> elements, the href attribute specifies the location (URL) of the external resource (most often a style sheet file).

Can we pass variable in href?

href”, append the variable to it (Here we have used a variable named “XYZ”). Then we need to append the value to the URL. Now our URL is ready with the variable and its value appended to it. In the example below, we will append a variable named 'XYZ' and its value is 55.

How do you make a dynamic href in HTML?

To Insert Dynamic Data into a URL You'll also need the Value of your Dynamic Tag, typically only seen within the HTML Source. Paste the Dynamic Tag Value into the URL of your link. You have successfully injected Dynamic Text into your hyperlink URL.


1 Answers

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
like image 191
Sarfraz Avatar answered Oct 16 '22 03:10

Sarfraz