jqueryajaxhtmltext

convert html to plain text jquery .ajax


Please find the below jQuery code. Sending email perfectly, but I see a whole bunch of code (HTML) in the email. I want to convert the html() output of #right to text(). How can I do that?

$(document).ready(function () {
    $('.confirm_button').click(function () {
        var student_name = $.trim($('#student_name').val());
        var student_contact = $.trim($('#student_contact').val());
        var student_email = $.trim($('#student_email').val());
        var right = $('#right').html();

        if (student_name.length < 3 || student_contact.length < 8 || student_email.length < 4)
        {
            $('#sendfail').attr('title', 'Sending Failed!').text('Please Enter valid information. All fields are required').dialog({
                buttons: {
                    'Ok': function () {
                        $(this).dialog('close');
                        $location.reload(true);
                    }
                },
                closeOnEscape: true,
                draggable: false,
                resizable: false,
                modal: true
            });
        }
        else
        {
            var objAjaxRequestData = {
                'student_name': student_name,
                'student_contact': student_contact,
                'student_email': student_email,
                'right': right
            };
            $.ajax({
                type: 'POST',
                url: 'send-select-university.php',
                data: objAjaxRequestData,
                success: function () {
                    $('#sendsuccess')
                            .attr('title', 'Message sent successfully')
                            .text('Your message has been sent. We will be in touch soon')
                            .dialog({
                                buttons: {
                                    'Ok': function () {
                                        $(this).dialog('close');
                                        window.location = "http://www.ankooverseas.com";
                                    }
                                },
                                closeOnEscape: true,
                                draggable: false,
                                resizable: false,
                                modal: true
                            });
                },
                error: function () {
                    alert('Oops request sending failed. Please check your internet connection and try again');
                }
            });
        }

    });
});

Solution

  • You need to replace

    var right=$('#right').html();

    to the

    var right=$('#right').text();

    According to your comments, I think you would be better to use the following code to have the selected items in a formatted view:

    var right = "";
    $("#shortlist_univs").children().each(function (i, univ) { // iterate over all universities and add them to the output variable in a list-like view.
      right += $(univ).text() + "\n"
    });