William William - 2 months ago 7
Ajax Question

jquery object need to fetch particular ID value from Ajax data

Using Ajax able to receive the data. Now need to fetch particular id value.

Here is the code:

$(document).on('click', '.userdetails', function () {
var memberid = $(this)[0].id;

var memberName = $("#memberName").val();

var mydata = 'task=' + "memberdetail" + '&ID='
+ memberid + '&current_user=' + "<?php echo $current_user->ID; ?>"
+ '&username=' + username;

$.ajax({
url: '<?php echo get_site_url(); ?>/userrdetail.php',
data: mydata,
type: 'POST',
cache: false,
beforeSend: function () {
$('body').scrollTop(0);
$(".loading").removeClass("hide");
},

success: function (data) {
alert(data)
}


Output:

<div>
<h4>William Mark</h4>
<input type="hidden" id="username" value="William Mark" name="username"> (999) 999-999 Ext: 454 &nbsp
</p>
</div>


Need to got id="username" value.

Answer

By using Data attributes like :

<div id="454" data-info="content that need to displayed"></div>

$(document).on('click', '.userdetails', function () {
var memberid = $(this)[0].id;
var username=$(this).data('info');
var memberName = $("#memberName").val();
var mydata = 'task=' + "memberdetail" + '&ID='
    + memberid + '&current_user=' + "<?php echo $current_user->ID; ?>"
    + '&username=' + username;

$.ajax({
    url: '<?php echo get_site_url(); ?>/userrdetail.php',
    data: mydata,
    type: 'POST',
    cache: false,
    beforeSend: function () {
        $('body').scrollTop(0);
        $(".loading").removeClass("hide");
    },

    success: function (data) {
    $("#username").html(username);                  
    $("#username").css({ 'display': "inline-block" });
    }