Remove File and value from Javascript

Tags: , , ,



May I ask about how can I remove the value of q , when the class .close is clicked? Here with my source code:

$(document).on('click', '.close', function () {
    $(this).parents('p').remove();

})

$('#Q1DocPath').change(function () {

    var path = $(this).val();

    if (path != '' && path != null) {
        var q = path.substring(path.lastIndexOf('\') + 1);

        $('#lblQ1Doc').html('<br/>' + '<p>' + q + '<a class="close"><font color="red">x</font><a>' + '</p>');

    }
})

Answer

If I understand the question correctly you need to remove the value of q only from HTML. The easiest way is to wrap the value with the span tag.

$(document).on('click', '.close', function () {
    $(this).prev('span').remove();

})

$('#Q1DocPath').change(function () {

    var path = $(this).val();

    if (path != '' && path != null) {
        var q = path.substring(path.lastIndexOf('\') + 1);

        $('#lblQ1Doc').html('<br/>' + '<p><span>' + q + '</span><a class="close"><font color="red">x</font><a>' + '</p>');

    }
})

Let me know if you needed something else or injected HTML can’t be modified, I’ll correct the answer.



Source: stackoverflow