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?
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?
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/
$(function(ready){
alert($('#Anchor').attr('href')); // prints Myproject/Mypage.aspx?tag=asp
alert($('#Anchor').text()); // prints Go
});
http://jsfiddle.net/max6s/
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'."
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