Skip to content
Advertisement

Remove File and value from Javascript

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>');

    }
})

Advertisement

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement