Change values in the html page using javascript

Asked

Viewed 286 times

-2

I have this form that is of a box office in html, but I want it to work using javascript, when clicking the button of + or - to change the quantity and change the total value.

I looked it up and saw the tag output I was testing but I could not perform a good implementation with the buttons...

formulario bilheteira

the html code I have is as follows:

<tr class="linha-meio borda-topo">
        <td class="divisao-direita">CRIANÇA (3 - 12 ANOS)</td>
        <td>13,78 €</td>
        <td class="alinhar-direita">
            <button class="botao-quantidade">&#10134;</button>
            <a class="quantidade">0</a>
            <button class="botao-quantidade">&#10133;</button>
        </td>
    </tr>
    <tr class="linha-meio">
        <td class="divisao-direita">ADULTO (13 - 64 ANOS)</td>
        <td>21,38 €</td>
        <td class="alinhar-direita">
            <button class="botao-quantidade">&#10134;</button>
            <a class="quantidade">2</a>
            <button class="botao-quantidade">&#10133;</button>
        </td>
    </tr>
    <tr class="borda-fundo">
        <td class="divisao-direita">SÉNIOR (+65 ANOS)</td>
        <td>15,20 €</td>
        <td class="alinhar-direita">
            <button class="botao-quantidade">&#10134;</button>
            <a class="quantidade">0</a>
            <button class="botao-quantidade">&#10133;</button>
        </td>
    </tr>
    <tr class="nao-mudar-cor-1">
        <td></td>
        <th class="alinhar-direita">Total:</th>
        <td class="alinhar-direita">42.76€ c/iva</td>
    </tr>
</table>

If you can help, I’m new to Java

  • 1

    "I saw the tag output" tag output? I don’t know this and I didn’t see your question, can you put an example? I was curious. About your question, you have created some code javascript? create a Function associated with the "click" event of the buttons, and according to the button (+/-) you sum or subtract the value and change in the "quantity" element. By the way, where does the amount need to be a link? could not be a span or div?

  • @Ricardopunctual I saw on this link link I can do a few things, but I don’t think it’s ideal here... I have already been able to put the quantity to work with the answer below, it is only missing to calculate the total according to the selected quantity

1 answer

0


Hello I will make an example here of how could be the solution, but there are several ways to solve the problem.

In my example I added id in the following elements, you should possibly use id in all elements.

 <a id="qtdcrianca" class="quantidade">0</a>
 <button id="addqtdcrianca" class="botao-quantidade">&#10133;</button>
 <td id="agregadototal" class="alinhar-direita">42.76€ c/iva</td>

Here is the script part.

<script>
   function adicionarValorCrianca() {
     let el = document.getElementById("qtdcrianca")
     let valor = parseInt(el.text)
     valor += 1
     el.text = valor
     atualizarTotal()
   }
   function atualizarTotal() {
     let total = 0
     let el = document.getElementById("qtdcrianca")
     let valor = parseInt(el.text)
     const totalcrianca = 13.78
     total = valor * totalcrianca
     const adulto = 21.38
     let el1 = document.getElementById("qtdadulto")
     let valor1 = parseInt(el1.text)
     total += valor1 * adulto
     console.log(total)
    const totalcstr = document.getElementById("agregadototal").textContent = total.toFixed(2)+'€ c/iva'
   }
   elemento = document.getElementById("addqtdcrianca")
   elemento.addEventListener("click", adicionarValorCrianca); 
</script>

In order to update the value, at the end of the increment method you must call the routine that updates the total value. I updated the html and script.

This would be a simple implementation of your requirements, you can improve much more, this example including creating a shared method to add or decrease values.

I hope I’ve helped.

  • Very good !! the buttons are working, I was able to understand the code and I have replicated it to the other columns. But now I’m missing the part where I calculate the full amount.... Could you give me a hand?

  • I updated with an idea of implementation, I don’t know if you can use variables declared within js. if you can’t, you’ll have to take an extra step to take the elements and do the parse for integer, before doing the sum.

  • I’ve already implemented the code, but of course the full amount needs updating, how am I supposed to do that? whenever there is a change on the page or with a function that checks every second? and to update the value is the function atualizarTotal that I have to call?

  • If you check I updated the answer. In Function adicionarValorCrianca()i added a new line, which is the call to update the total value. The total value should be updated whenever there is a button click.

  • Yes, I saw it but then I forgot to pass that part. Everything is working very well, thank you. Now there is still an improvement possible, would not let the numbers become negative, but so it is already good, thanks for the help

Browser other questions tagged

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