Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
how to use javascript functions multiple times

I'm running a code where I must create a function and call it multiple times. I am having trouble calling it more than once. how to use Mul() function in second table row. Please help Me. Thanks.

function Mul() {
  var quantity = document.getElementById("quantity").value;
  var price = document.getElementById("price").value;
  document.getElementById(id).value = quantity * price;
}
<table class="table table-center table-hover" id="myTable">
  <thead>
    <tr>
      <th>Description</th>
      <th>Quantity</th>
      <th>Unit Price</th>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="text" class="form-control">
      </td>
      <td>
        <input type="number" id="quantity" class="form-control" onkeyup="Mul()">
      </td>
      <td>
        <input type="number" id="price" class="form-control" onkeyup="Mul()">
      </td>
      <td>
        <input type="text" id="amount" class="form-control" disabled>
      </td>
    </tr>
    <tr>
      <td>
        <input type="text" class="form-control">
      </td>
      <td>
        <input type="number" id="quantity" class="form-control">
      </td>
      <td>
        <input type="number" id="price" class="form-control">
      </td>
      <td>
        <input type="text" id="result" class="form-control" disabled>
      </td>
    </tr>
  </tbody>
</table>

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Add class name for every input and pass index to Mul() function try below code.

function Mul(index) {
  var quantity = document.getElementsByClassName("quantity")[index].value;
  var price = document.getElementsByClassName("price")[index].value;

  document.getElementsByClassName("amount")[index].value = quantity * price;
}
<table class="table table-center table-hover" id="myTable">
  <thead>
    <tr>
      <th>Description</th>
      <th>Quantity</th>
      <th>Unit Price</th>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="text" class="form-control">
      </td>
      <td>
        <input type="number" id="" class=" quantity form-control" onkeyup="Mul('0')">
      </td>
      <td>
        <input type="number" id="" class="price form-control" onkeyup="Mul('0')">
      </td>
      <td>
        <input type="text" id="amount" class="amount form-control" disabled>
      </td>
    </tr>
   <tr>
      <td>
        <input type="text" class="form-control">
      </td>
      <td>
        <input type="number" id="" class=" quantity form-control" onkeyup="Mul('1')">
      </td>
      <td>
        <input type="number" id="" class="price form-control" onkeyup="Mul('1')">
      </td>
      <td>
        <input type="text" id="amount" class="amount form-control" disabled>
      </td>
    </tr>
    <tr>
      <td>
        <input type="text" class="form-control">
      </td>
      <td>
        <input type="number" id="" class=" quantity form-control" onkeyup="Mul('2')">
      </td>
      <td>
        <input type="number" id="" class="price form-control" onkeyup="Mul('2')">
      </td>
      <td>
        <input type="text" id="amount" class="amount form-control" disabled>
      </td>
    </tr>
  </tbody>
</table>

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda