Set the value of a TD using a JS function

Asked

Viewed 149 times

0

I have the following function, which returns an entire value of an AJAX query to my SQL Server database.

function qtdItensUltimaCompra(){

        var url = 'AjaxQtdProdutoInsumo.jsp';
        var temp = document.getElementsByName("cdItemInsumo");
        var cd_unidade = document.getElementsByName("cdUnidade");
        var cdSetor = getElementsByName("cdSetor");
        var param = "&cdInsumo=" + temp + "&cd_unidade=" + cd_unidade + "&cdSetor=" + cdSetor;


        var qtdUltimaCompra = loadAjax(url,param);
        this.setAttribute('qtdUltimaCompra', texto);
}

And here the TD that this value needs to appear, however I could not through the setAttribute nor the document.qtdUltimaCompra.value(qtdUltimaCompra), any suggestions?

<td align="center" width="15%" class="font-padrao"  id="tdQtdUltimaCompra">
<input type="hidden" name="qtdUltimaCompra" value="">

3 answers

1

To put the value on <td>:

document.getElementById('tdQtdUltimaCompra').innerHTML = qtdUltimaCompra

The tag <td> does not have a value attribute. What you can change is its content using innerHTML. But with this you will vanish with the <input> that apparently you have inside your <td>.

If that’s not the case and you want to continue with this <input> you must take out the attribute hidden it contains, add an id like id="txtQtdUltimaCompra" for example, for better manipulation, and use the code below to change its value:

document.getElementById('txtQtdUltimaCompra').value = qtdUltimaCompra

0

Hello,

Use innerHtml like this

function atualiza() {
 document.getElementById('tdQtdUltimaCompra').innerHTML="novo valor";
}

0

To insert text within an element other than an input, you must use the innerText attribute, as follows:

document.getElementById("qtdUltimaCompra").innerText = "Texto"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.