mark_spencer
mark_spencer

Reputation: 393

Get value of text field in clicked div

I have list of divs that generated by ajax call

Here is code

 function todolisttoday() {
    $("#list").empty();
    $("#counter").empty();
    $.ajax({
        url: '@Url.Action("GetTodayList", "Home")',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(),
        type: 'POST',
        dataType: 'json',
        processData: false,
        success: function (data) {
            var list = data;
            var count = Object.keys(data).length;
            $("#counter").append('<b>' + "You have" + " " + count + " " + "appointments today" + '</b>');
            for (var i = 0; i <= list.length - 1; i++) {
                var timeobject = new Date();
                timeobject.setTime(Date.parse(list[i].time));
                var hours = timeobject.getHours();
                var minutes = timeobject.getMinutes();
                var ampm = hours >= 12 ? 'pm' : 'am';
                hours = hours % 12;
                hours = hours ? hours : 12; // the hour '0' should be '12'
                minutes = minutes < 10 ? '0'+minutes : minutes;
                var strTime = hours + ':' + minutes + ' ' + ampm;
                var divslist = '<div class="listdiv">' +
                    '<b >' + (i + 1) + '</b>' + "." + " "
                    + '<b class="title">' + list[i].title + '</b>' + " "
                    + '<b >' + strTime + '</b>' + '<b class="status">' + list[i].status +'<b>' 
                    + '</div>';
                $("#list").append(divslist);
            };
        }
    });
};

They are generated by this code

var divslist = '<div class="listdiv">' +
                    '<b >' + (i + 1) + '</b>' + "." + " "
                    + '<b class="title">' + list[i].title + '</b>' + " "
                    + '<b >' + strTime + '</b>' + '<b class="status">' + list[i].status +'<b>' 
                    + '</div>';

I need to get value of '<b class="status">' + list[i].status +'<b>'

When I click on div

I make div click event

Here is code

 $(document).on('click', '.listdiv', function() {
    var appointmentTitle = $(this).text();

});

But its get all data from div.

How I can get only class="status" value?

Upvotes: 3

Views: 371

Answers (2)

kyun
kyun

Reputation: 10264

$(document).on('click', '.listdiv', function() {
    var appointmentTitle = $(this).text();
    console.log($(this).find('.status').text());
});

try this.

Upvotes: 1

Dave
Dave

Reputation: 10924

Try this:

$(document).on('click', '.listdiv.status', function() {
    var appointmentTitle = $(this).text();
});

This will only get the divs with both the listdiv and status classes.

Upvotes: 2

Related Questions