How to pass what runs in JS to input field on the same page?

Asked

Viewed 35 times

0

I need some help that I’m stuck in here, and I don’t understand AJAX. I have these fields, select(tour), QTE(Adults) QTE(Child) TOTAL ( where performs the calculation). I need that when calculating, when clicking on the icon, also pass to another form, the values: TOUR and the calculated TOTAL, have to do this, do not want to send to another page.

<td>
<div>
<select class="form-control" name="tour" id="tour">
<option>Choose a tour</option>
<option value="98">Douro Valley Tour</option>
<option value="118">Bairrada Wine Tour</option>
</select>
</div>
</td>
<td>
<div class="numbers-row">
<input type="text" value="1" id="adults" class="qty2 form-control" name="adults">
</div>
</td>
<td>
<div class="numbers-row">
<input type="text" value="0" id="child" class="qty2 form-control" name="child">
</div>
</td>
<td>
<span id="soma"></span>
</td>
<td class="options">
<a href='javascript:calcular();'><i class="icon-ccw-2"></i></a>
</td>
</tr>
</tbody>
</table>
<script>
    function calcular() {
        var unit = "45";
        var tour = Number(document.getElementById("tour").value);
        var adults = Number(document.getElementById("adults").value);
        var child = Number(document.getElementById("child").value);
        var elemResult = document.getElementById("soma");

        if (elemResult.textContent === undefined) {

        elemResult.textContent = String(tour * adults + unit * child)+ ".00€";
        }
        else { // IE
        elemResult.innerText = String(tour * adults + unit * child) + ".00€";
            }
        }
</script>

<input type="hidden" name="item_name" value="SELECT">
<input type="hidden" name="amount" value="TOTAL">
  • 1

    Explain better what you mean by "pass to another form".

  • I just want to pass the value of select and the ID field where it shows Total, to the fields below....

  • Start by placing an id in these fields. Then you can assign value with document.getElementById('id-do-input').value = 'valor desejado';.

  • My JS is very bad: the 'desired value' to pass the value of select which would be?

  • You tell me. Wouldn’t be the values you already have in the variable tour, in one of the inputs, and the result of your account in the other?

  • yes, but then I only pass like . value='tour' --- tour

  • Solved...thank you friend

Show 2 more comments

1 answer

0

Arrow the innerText or innerHTML of the result you have had to wherever you want it to appear.

Ex: const resultado = document.querySelector('.resultado-achado').innerHTML;

In the field, text or other form would be: document.querySelector('.outro-texto').innerHTML = resultado

  • can show me an example cesar???

  • I posted the example in my reply. Your input needs to have an id or class, and that’s where I put it in the example as . result-find, you enter the name of your input. To understand you need to understand 1 little javascript.

  • already solved, thank you very much for the help.

Browser other questions tagged

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