Sascha C Sascha C - 6 months ago 12
Javascript Question

Using Parameter in Ajax jQuery / Javascript

I am trying to use jQuery. To update a specific TD with innerHTML I am trying to figure out, how I can catch the value of the parameter.

In this example i need to catch the user-id "1234" so i can update the TD with the ID "status1234".

I am not very familar with javascript, I hope someone can give me a hint.



$(function() {
$('.ajax-link').click(function() {
$.get($(this).attr('href'), function(msg) {
alert("Data Saved: " + msg);
$("#status" & user).html("some content");
});
return false;
});
});

// TD with Link:

<td id="status1234">
<a href="/ajax_test.cfm?user=1234" class="ajax-link">Do the Ajax</a>
</td>





Thanx!

Answer

You may do like this

       $(function() {
  $('.ajax-link').click(function() {
  var user=$(this).data('user');
    $.get("/"+$(this).data('url')+"/user="+user, function(msg) {
      alert("Data Saved: " + msg);
      $("#status" + user).html("some content");
    });
  
  });
   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td id="status1234">
      <a data-url="ajax_test.cfm" data-user="1234" class="ajax-link">Do the Ajax</a>
    </td>