Change value <td> dynamically with Javascript

Asked

Viewed 643 times

0

Good afternoon Personal,

I have the following radios in HTML

<input class="form-check-input" type="radio" name="filtro_por" id="filtro_por" value="sinistro_datacadastro" >
<label class="form-check-label" >Data de Cadastro</label>

<input class="form-check-input" type="radio" name="filtro_por" id="filtro_por" value="sinistro_datasinistro" checked>
<label class="form-check-label">Data de Sinistro</label>

And I need that every time the user selects one of the options the date value in td below, automatically change.

<td>{{ obj.sinistro_data }}</td>

I would like to make this change dynamically using Jquery, because my project was developed in Bootstrap.

  • Is it a column for date of registration and another for date of claim? Or is it the same column for both dates? Is the source for the date one for the claim and the other for the registration or the same source for both? Can the current date be used or it comes from a Datepicker?

  • Good morning Augusto... - It is a column for each date (registration and sinister)... - I didn’t understand your question about the source, but basically, I would like that as I select the respective radios, td change and search in the field according to the filter I select - As I have a column for each date, and they are already registered, I do not want to use the current date...

  • 1

    I asked about the data source because I didn’t understand if the data is present on the page (input or Datepicker) or if we need to request this data from a server. If a back end request is the case, ask the question the means you use to request the dates.

2 answers

1

Using HTML and Javascript you can change the content with this command:

document.getElementById("nomedoelemento").innerHTML = "novo valor aqui";

A Case:

<html>
<body>    
<table>
    <tr>
        <td>Sinistro: <span id="sinistro_data"></span></td>
    </tr>
</table>    
<input type="button" onclick="mudaHTML()" value="Muda HTML" />
<script>    
function mudaHTML(){
    document.getElementById("sinistro_data").innerHTML = "novo valor aqui" + Date.now();
}
</script>
</body>
</html>

0

This can be done by adding the values into one data attribute and alternate them using the .on("change")

When changing the radio, the change event will be triggered. This event checks the value of data-value of the radio selected and assigned to the element on the screen. To find this element, just add an id for that element and use the Jquery selector to insert the content into it.

$("input[name='filtro_por']").on("change", function(){
    $("#destino").html($("input[name='filtro_por']:checked").data("value"));
});
<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>

<input class="form-check-input" type="radio" name="filtro_por" value="sinistro_datacadastro" data-value="24/09/2019" >
<label class="form-check-label" >Data de Cadastro</label>

<input class="form-check-input" type="radio" name="filtro_por" value="sinistro_datasinistro"  data-value="23/09/2019" checked>
<label class="form-check-label">Data de Sinistro</label>
<br>
<table>
    <tr>
         <td id="destino"></td>
    </tr>
</table>

  • Good afternoon Jrd, I made the change, but the column td, returns empty, when I change between the options... The filter, works, but the column returns no value, as if it was not changed from td to call the respective table of the database.

  • @Carlossouza The idea is to bring the dates ready from the backend and insert them in the data-value of inputs so that they can be modified

  • In fact, the idea is to toggle the td so that dynamically it looks in the database for different columns of the same table

Browser other questions tagged

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