0

I want to pass two url parameter to a page but its only able to pass the first you URL. Here's an example html users?user_id=9&username=john, with this example the script only passes the first one parameter which is 9. I get it in an alert "9&username=" without showing the value of the username

$(document).ready(function () {
    $('.usersclick').on('click', function (event) {
        $('#users').empty();
        var href = $(this).attr('href'); // Get the `href` value of the clicked anchor
        var paramValue = href.split('=')[1]; // Get the value after =
        $('#news_container').load('http:users?user_id='+paramValue);
        alert (paramValue)
i_user
  • 511
  • 1
  • 4
  • 21

2 Answers2

2

You don't need to separate out the parameters, just get the whole query string ("?user_id=9&username=john" in your example) and put in the URL:

$(document).ready(function () {
  $('.usersclick').on('click', function (event) {
    $('#users').empty();
    var href = $(this).attr('href'); // Get the 'href' value of the clicked anchor
    var query = href.substr(href.indexOf('?')); // Get '?' and the rest
    $('#news_container').load('http:users' + query);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0
$(document).ready(function () {
$('.usersclick').on('click', function (event) {
    $('#users').empty();
    var href = $(this).attr('href'); // Get the `href` value of the clicked anchor
    var i = hred.indexOf('='); // get the index of the very first '='
    var paramValue = href.substr(i+1); // Get the rest of the string after the first '=' , regardless of how many parameters are there
    $('#news_container').load('http:users?user_id='+paramValue);
    alert (paramValue)
maaeab
  • 326
  • 1
  • 7