tags:

views:

73

answers:

4

I am having an anchor link in aspx page like:

<a id="Anchor"class="myAnchor" href="Myproject/Mypage.aspx?myTag=asp">Go</a>

I need to access the "myTag" value using jquery.How to do this?

+3  A: 
$(function(ready){
    alert($('#Anchor').attr('href')); // prints Myproject/Mypage.aspx?tag=asp
    alert($('#Anchor').text()); // prints Go
});

http://jsfiddle.net/max6s/

Marwelln
+1  A: 

To get the href of the link:

var href = $('#Anchor').attr('href');

To get the HTML inside:

var html = $('#Anchor').html();

#Anchor is the CSS-format selector that means, "Select the element with the ID 'Anchor'."

idealmachine
+1  A: 

You could do this:

var myTag = $('#Anchor')[0].search.split('=')[1];

Example: http://jsfiddle.net/B6GYB/

Or not using jQuery:

var myTag = document.getElementById('Anchor').search.split('=')[1];

Example: http://jsfiddle.net/B6GYB/1/

patrick dw
@patrick dw really a great one.Thanks a lot.
ANP
@patrick dw but one thing suppose 'myTag=asp net', then it gives result 'asp+net'. One + symbol appears, is there any way so that out put will be 'asp net' only?
ANP
@ANP - Sure, just add `.replace('+', ' ')` to the end.
patrick dw
+1  A: 

You can get the specific query parameter of url by using following code:

Javascript

<script type="text/javascript">
        function getAnchorValue(anchorId, key) {
            var href = document.getElementById(anchorId).getAttribute('href');
            var pageQuerySearch = new PageQuery(href.split('?')[1]);
            return unescape(unescape(pageQuerySearch.getValue(key)));
        }
        function PageQuery(query) {
            if (query.length > 1) {this.q = query; } else { this.q = null; } this.keyValuePairs = new Array();
            if (this.q) { for (var i = 0; i < this.q.split("&").length; i++) { this.keyValuePairs[i] = this.q.split("&")[i]; } };
            this.getValue = function (s) {
                for (var j = 0; j < this.keyValuePairs.length; j++) {
                    if (this.keyValuePairs[j].split("=")[0] == s) { return this.keyValuePairs[j].split("=")[1]; }
                } return false;
            };
        }
</script>

and here is the usage of this function:

alert(getAnchorValue('Anchor', 'myTag'));

JQuery

<script type="text/javascript">
    ; (function ($) {
        $.extend({
            getAnchorValue: function (name, url) {
                function getQueryStringParams() {
                    var parameters = {}, e, a = /\+/g, r = /([^&=]+)=?([^&]*)/g,
                        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
                        q = url ? url.split('?')[1] : window.location.search.substring(1);
                    while (e = r.exec(q)) { parameters[d(e[1])] = d(e[2]) }; return parameters;
                }
                if (!this.params) this.params = getQueryStringParams();
                return this.params[name];
            }
        });
    })(jQuery);
</script>

Usage:

alert($.getAnchorValue('myTag', $('#Anchor').attr('href')));

EDIT: I have editted my answer and also added the jquery code for getting the querystring parameter

Zain Shaikh
not bad, but it's not jQuery
KARASZI István
jQuery was not the requirement. the requirement was to get the value of tag.
Zain Shaikh
@Zain: "I need to access the "myTag" value using jquery.How to do this?" - It was required.
Marwelln
@Marwelln, @KARASZI István, I have editted my answer and added the jquery function to get the parameter value from an anchor's href.
Zain Shaikh