How to change HTML table cell value (ie: if i enter quantity in the text box it should reflect qty colm of pol(last table value ) ) using JavaScript

How to change HTML table cell value (ie: if i enter quantity in the text box it should reflect qty colm of pol(last table value ) ) using JavaScript



enter image description here



I need to get the value from the textbox(Discount or quantity) and need to change the last column value of the quantity or discount in the table


<div class="col-md-2">
<h5 class="bordercool"><b>Quantity</b></h5>
<input type="text" class="form-control text-uppercase" id="Quantity" placeholder="Quantity">
</div>
<div class="col-md-2">
<h5 class="bordercool"><b>Discount</b></h5>
<input type="text" class="form-control text-uppercase" id="Discount" placeholder="Discount">
</div>

<table id="sales_table" class="table table-bordered table-hover ">
<thead>
<tr class="tableheader">
<th style="width:40px">#</th>
<th style="width:250px">Item Name</th>
<th style="width:120px">Price</th>
<th style="width:60px">Disc %</th>
<th style="width:60px">Qty</th>
<th style="width:60px">GST %</th>
<th style="width:60px">GST AMT</th>
<th style="width:60px">CGST %</th>
<th style="width:60px">CGST AMT</th>
<th style="width:120px">Total</th>
<th style="width:px"></th>
</tr>
</thead>
<tbody></tbody>
</table>





Hi! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a Minimal, Complete, and Verifiable example of your attempt and say specifically where you're stuck. People will be glad to help.
– T.J. Crowder
Aug 26 at 15:08





I've removed the php and html tags since you're asking how to do this with JavaScript. This isn't a PHP or HTML question.
– T.J. Crowder
Aug 26 at 15:08





which column will be changed ?
– ths
Aug 26 at 15:08




2 Answers
2




let Quantity = document.getElementById('Quantity')
let Discount = document.getElementById('Discount')

Quantity.addEventListener('keyup',function(e)
document.querySelector('.tableheader > .columnQty').innerText = 'Qty '+ e.target.value +'%';

,false)
Discount.addEventListener('keyup',function(e)
document.querySelector('.tableheader > .columnDisc').innerText = 'Disc '+ e.target.value +'%';
,false)


<div class="col-md-2">

<h5 class="bordercool"><b>Quantity</b></h5>
<input type="text" class="form-control text-uppercase" id="Quantity" placeholder="Quantity">
</div>
<div class="col-md-2">
<h5 class="bordercool"><b>Discount</b></h5>
<input type="text" class="form-control text-uppercase" id="Discount" placeholder="Discount">
</div>

<table id="sales_table" class="table table-bordered table-hover ">
<thead>
<tr class="tableheader">
<th style="width:40px">#</th>

<th style="width:250px">Item Name</th>

<th style="width:120px">Price</th>

<th style="width:60px" class='columnDisc'>Disc %</th>
<th style="width:60px" class='columnQty'>Qty</th>
<th style="width:60px">GST %</th>
<th style="width:60px">GST AMT</th>
<th style="width:60px">CGST %</th>
<th style="width:60px">CGST AMT</th>
<th style="width:120px">Total</th>
<th style="width:px" class="changedItem"></th>
</tr>
</thead>
<tbody></tbody>
</table>





Thank You for your replay! I don't want to change the header. Instead of that I need to change the last value of discount column
– Sarath Das
Aug 26 at 17:57



this will capture the input and place it in a new row. just enter a number for quantity and hit enter. duplicate code for discount.

















var quantInput = document.getElementById('Quantity');




quantInput.addEventListener("keyup", function(event)
// Cancel the default action, if needed
// event.preventDefault();
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13)
var quantity =document.getElementById("Quantity");

var table = document.getElementById("sales_table");
var row = table.insertRow(1);
var cell = new Array(11);
var length= document.getElementById("sales_table").rows.length;
var row = table.insertRow(length);

for(var i = 0; i<11;i++)
cell[i] = row.insertCell(i);


cell[4].innerHTML = quantity.value;
document.getElementById("Quantity").value = '';

);


<div class="col-md-2">

<h5 class="bordercool"><b>Quantity</b></h5>
<input type="text" class="form-control text-uppercase" id="Quantity" placeholder="Quantity" >
</div>
<div class="col-md-2">
<h5 class="bordercool"><b>Discount</b></h5>
<input type="text" class="form-control text-uppercase" id="Discount" placeholder="Discount">
</div>

<table id="sales_table" class="table table-bordered table-hover ">
<thead>
<tr class="tableheader">
<th style="width:40px">#</th>

<th style="width:250px">Item Name</th>

<th style="width:120px">Price</th>

<th style="width:60px">Disc %</th>
<th style="width:60px">Qty</th>
<th style="width:60px">GST %</th>
<th style="width:60px">GST AMT</th>
<th style="width:60px">CGST %</th>
<th style="width:60px">CGST AMT</th>
<th style="width:120px">Total</th>
<th style="width:px"></th>
</tr>
</thead>
<tbody></tbody>
</table>





Thank You for your replay. while adding Quantity its adding new column. What I need is to change the existing quantity of last quantity value.
– Sarath Das
Aug 26 at 17:49





i don't understand. You have a blank table with just a row of th's What exactly do you want to change?
– DCR
Aug 26 at 19:31






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế