in my script there a simple list shows links for editing
<ul>
<li><a href="edit.php?id=5" class="edit">click here</a></li>
<li><a href="edit.php?id=5" class="edit">click here</a></li>
<li><a href="edit.php?id=5" class="edit">click here</a></li>
<li><a href="edit.php?id=5" class="edit">click here</a></li>
</ul>
what i need is to read the variable id so i can send it through .ajax call and i tried this function
$(document).ready(function(){
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1)
.split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
$('.edit').click(function(){
var test = getUrlVars()["id"];
alert(test);
});
});
When I clicked on the link, the alert message shows undefined
.
I tried another function:
$(document).ready(function(){
var urlParams = {};
(function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function(s) {
return decodeURIComponent(s.replace(pl, " "));
},
query = window.location.search.substring(1);
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
$('.edit').click(function(){
var test = urlParams["id"];
alert(test);
});
});
... but even this also shows up the alert message undefined
.