admin管理员组

文章数量:1310074

I am pretty new in JQuery and I have the following problem.

I have create this JQuery function that when the user select a file into an input tag having id=rendicontoAllegato it put the name of this file into an hidden div having id=nomeDocumentoRendicontazione into my page

$(document).ready(function() {

    $("#rendicontoAllegato").change(function() {
        alert("DOCUMENTO dopo selezione: " + $("#nomeDocumentoRendicontazione").text());

        var selectedFileName = $("#rendicontoAllegato").val();

        $('#nomeDocumentoRendicontazione').append(selectedFileName);

    });
 });

It works fine but the only problem is that if I first select something asfile1.txt and then select another file as file2.txt into the div having id=nomeDocumentoRendicontazione I will have the concatenation of the 2 files name.

So I will have something like file1.txtfile2.txt and it is not good for me.

How can I replace the value of the div having id=nomeDocumentoRendicontazione instead to append a new value inside it?

I am pretty new in JQuery and I have the following problem.

I have create this JQuery function that when the user select a file into an input tag having id=rendicontoAllegato it put the name of this file into an hidden div having id=nomeDocumentoRendicontazione into my page

$(document).ready(function() {

    $("#rendicontoAllegato").change(function() {
        alert("DOCUMENTO dopo selezione: " + $("#nomeDocumentoRendicontazione").text());

        var selectedFileName = $("#rendicontoAllegato").val();

        $('#nomeDocumentoRendicontazione').append(selectedFileName);

    });
 });

It works fine but the only problem is that if I first select something asfile1.txt and then select another file as file2.txt into the div having id=nomeDocumentoRendicontazione I will have the concatenation of the 2 files name.

So I will have something like file1.txtfile2.txt and it is not good for me.

How can I replace the value of the div having id=nomeDocumentoRendicontazione instead to append a new value inside it?

Share Improve this question asked Nov 27, 2015 at 11:50 AndreaNobiliAndreaNobili 43.1k122 gold badges361 silver badges678 bronze badges 1
  • You can use .html() or .text() or .empty().append() or .empty().prepend() .... google.co.uk/search?btnG=1&q=jquery+set+content+of+div – ahervin Commented Nov 27, 2015 at 12:00
Add a ment  | 

3 Answers 3

Reset to default 5

You can use the .text() fn if you are dealing with with your data to be inserted as text or .html() fn if you are dealing with html to be replaced

$('#nomeDocumentoRendicontazione').text(selectedFileName);

Or

$('#nomeDocumentoRendicontazione').html(selectedFileName);

Use html() instead of append().

$(document).ready(function() {

    $("#rendicontoAllegato").change(function() {
        alert("DOCUMENTO dopo selezione: " + $("#nomeDocumentoRendicontazione").text());

        var selectedFileName = $("#rendicontoAllegato").val();

        $('#nomeDocumentoRendicontazione').html(selectedFileName);

    });
 });

You have to use

 $('#nomeDocumentoRendicontazione').html(selectedFileName);

it will replace the already present HTML of that. OR you can use

 $('#nomeDocumentoRendicontazione').text(selectedFileName);

it will do the same but append your data as text.

本文标签: javascriptHow can I replace the content of a div instead append a value using JQueryStack Overflow