Can't update textarea with javascript after writing to it manually

人走茶凉 提交于 2019-12-04 00:23:25

I had this same problem. When HTML is parsing to DOM object, content of textarea is innetHTML of <textarea>

<textarea cols="10" rows="10">Content of textarea</textarea>

Values document.getElementByTagName('textarea').innerHTML and document.getElementByTagName('textarea').value returns this same value Content of textarea

But after loading DOM object, changing innerHTML value doesn't change content of textarea box. A solution to change it is modifying value field.

document.getElementByTagName('textarea').value="New content of textarea";

or in jquery

$("textarea").val("New content of textarea");

.value and .innerHTML seemed to fix the problem

I have this exact problem. So far I have tried

$('#addofferlink').click(function(){
    var offer = "#OFFER_"+$('#offer_id').val()+"#";
    $('#message').append(offer);
});

I have also tried,

    $('#addofferlink').click(function(){
    var e = $('#message').html();
    var offer = "#OFFER_"+$('#offer_id').val()+"#";
    $('#message').html(e+offer);
});

The problem seems to be that if the user types into the textarea (#message) then clicks the button to add the offer link, nothing is displayed. Looking at the source, using Firebug, I can see that the actual text contained in the textarea is being amended, but not displayed on the screen.

It seems that the javascript is changing the html contents of the tag, but the browser is storing the currently typed contents in memory (i assume) or elsewhere.

Updating the textarea using val(e+offer) will remove the entered text and replace it with the offerlink, and using innerHTML = e+offer doesn't work at all, showing no change in the textarea.


I've managed to sort this!

My thinking revolved around the fact that because tag contains the text, and has it's own closing tag, that I would have to get it's contents using .html().

By using .val() to get the contents of the field you can then pull and update the contents simply. I ended up with the following,

    $('#addofferlink').click(function(){
    var e = $('#message').val();
    var offer = "#OFFER_"+$('#offer_id').val()+"#";
    $('#message').val(e+offer);
});
Dornail

I fixed this odd problem simply by:

document.forms[0].text_area_name.value = "STUFF";

Even after modifying the textarea the script will overwrite.

innerHTML didn't work for me after modifying the textarea.

Sometimes you may wish to display in a textarea, a particular string and let the user see what it is, then possibly make changes to it; but, you wish to enable them to see what the original text was. Textareas are pretty unwieldy, but even a manual change in the edited text is reversable using this setup.

<button id="ShowNOWxItmDscptn" onClick="EditItmDesc(1)">What is it now?</button>
<button id="OpnTAxWriteNewEdit" onClick="EditItmDesc(2)">Write / Edit</button>
<textarea id="RUBYxDescribe" max="500" width="75" placeholder="Magic"> </textarea>

// a global variable:
// UVarDescriptionTxt = ("This is the first line.\r\nSecond line!");

function EditItmDesc(x){
    console.log("*x* = "+x+".");
    console.log("Text to reset : "+UVarDescriptionTxt+".");

    var InnyShow = document.getElementById("RUBYxDescribe");
    InnyShow.value = UVarDescriptionTxt;

    if(x == 1) {
        InnyShow.innerHTML = "";
        InnyShow.textContent = "";
        InnyShow.innerHTML = UVarDescriptionTxt;
    }

    if(x == 2){
        InnyShow.value = "";
    }
}

The above structure solved that problem for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!