my table looks something like that:
<table class="tg">
<tr>
<th class="tg-031e"></th>
<th class="tg-031e"></th>
<th class="tg-031e"></th>
<th class="tg-031e"></th>
</tr>
<tr>
<td class="tg-031e"><span class="display-mode" />
<input class="edit-mode max" value="1" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="edit-mode min" value="2" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="edit-mode width" value="3" />
</td>
<td class="tg-031e">
<button name="Klickmich" type="button" value="Überraschung">Foo</Button>
</td>
</tr>
<tr>
<td class="tg-031e"><span class="display-mode" />
<input class="max edit-mode" value="4" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="min edit-mode" value="5" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="edit-mode width" value="6" />
</td>
<td class="tg-031e">
<button name="Klickmich" type="button" value="Überraschung">Foo</Button>
</td>
</tr>
<tr>
<td class="tg-031e"><span class="display-mode" />
<input class="edit-mode max" value="7" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="edit-mode min" value="8" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="edit-mode width" value="9" />
</td>
<td class="tg-031e">
<button name="Klickmich" type="button" value="Überraschung">Foo</Button>
</td>
</tr>
When the user clicks on the row I want to save / fetch the values of the input values from the clicked row.
I did that with:
$("button").click(function () {
var min = jQuery("input#min").val();
var max = jQuery("input#max").val();
var width = jQuery("input#width").val();
console.log(currentClass);
console.log(min);
console.log(max);
console.log(width);
});
With this code I only get the values of the first row. I should revice the values from the clicked row. For example if the user clicks on the third row the values should be 7,8,9.
Also I'm wondering why I get with $(this).parents('tr') the button context instead of the tr context in jquery.
Does someone have a good workaround for this? Code also here http://jsfiddle.net/zW5KC/1/
Okay, so firstly your IDs should be unique, so I've changed them to classes.
Secondly, when you try var min = jQuery("input#min").val(); its finding an input with the id of min, however its just finding the first one on the page, it doesn't know which one you want.
So I've updated the code to use this which in this case references the actual button element you've clicked. From there we find the closest parent which is a tr and then from there find a child who has the correct class. Code;
<table class="tg">
<tr>
<th class="tg-031e"></th>
<th class="tg-031e"></th>
<th class="tg-031e"></th>
<th class="tg-031e"></th>
</tr>
<tr>
<td class="tg-031e"><span class="display-mode" />
<input class="max" class="edit-mode" value="1" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="min" class="edit-mode" value="2" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="width" class="edit-mode" value="3" />
</td>
<td class="tg-031e">
<button name="Klickmich" type="button" value="Überraschung">Foo</Button>
</td>
</tr>
<tr>
<td class="tg-031e"><span class="display-mode" />
<input class="max" class="edit-mode" value="4" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="min" class="edit-mode" value="5" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="width" class="edit-mode" value="6" />
</td>
<td class="tg-031e">
<button name="Klickmich" type="button" value="Überraschung">Foo</Button>
</td>
</tr>
<tr>
<td class="tg-031e"><span class="display-mode" />
<input class="max" class="edit-mode" value="7" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="min" class="edit-mode" value="8" />
</td>
<td class="tg-031e"><span class="display-mode" />
<input class="width" class="edit-mode" value="9" />
</td>
<td class="tg-031e">
<button name="Klickmich" type="button" value="Überraschung">Foo</Button>
</td>
</tr>
<script type="text/javascript">
$("button").click(function () {
var $row = $(this).closest('tr');
var min = $row.find('.min').val();
var max = $row.find('.max').val();
var width = $row.find('.width').val();
console.log(currentClass);
console.log(min);
console.log(max);
console.log(width);
});
</script>
Use closest() to find parent tr and then use find() to get the specific element inside that tr like this:
$("button").click(function () {
var min = jQuery(this).closest("tr").find("#min").val();
var max = jQuery(this).closest("tr").find("#max").val();
var width = jQuery(this).closest("tr").find("#width").val();
console.log(min);
console.log(max);
console.log(width);
});
Related
I'm trying to achieve two additions in my code.
I am trying to get the sum of the row values in the last column of the row
I am trying to get the sum of all the final columns in another div
I've achieved the first, my code gives me the correct value of each row. However the second sum is not populating the addition. It's giving me the same value of each row, but I want the value of all rows.
$('.bonus-sum').keyup(function() {
var sum = 0;
var salary = parseFloat($(this).closest('tr').find('.wagein').text());
var bonus = parseFloat($(this).closest('tr').find('.bonus-sum').val()) || 0;
$(this).closest('tr').find('.bonus-in:checked').each(function() {
sum = salary + bonus;
});
$(this).closest('tr').find('.netpay').text(sum.toFixed(2));
var netpay = 0;
netpay += sum;
$('#total').text('₹ ' + netpay.toFixed(2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<table>
<thead>
<tr>
<th class="text-center"><input type="checkbox" checked="checked" class="checkAll" name="checkAll" /></th>
<th>#</th>
<th>Beneficiary Name</th>
<th class="text-right box">Bonus ₹</th>
<th class="text-right">Salary ₹</th>
<th class="text-right">Net pay ₹</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>1</td>
<td>Chellammal Kochimoni</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>2</td>
<td>Christal Prema G.</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>3</td>
<td>Kamalesan T.</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>4</td>
<td>Palammal A.</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" id="bene_id" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>4</td>
<td>Thangapazham</td>
<td><input type="text" id="bonus_temp" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
</tbody>
</table>
</div>
<div>Total Net Pay <span id="total">₹ 0.00</span></div>
To fix this you need to create a loop which iterates through all the .netpay elements, not just the one which was updated, and generates the total value.
In addition, you need to perform the same action when the checkbox is changed. The logic itself can also be made more succinct and DRY, like this:
$('.bonus-sum').on('input', updateRowTotal);
$('.bonus-in').on('change', updateRowTotal).trigger('change'); // trigger is to set values on load
function updateRowTotal() {
let $tr = $(this).closest('tr');
let salary = parseFloat($tr.find('.wagein').text()) || 0;
let bonus = parseFloat($tr.find('.bonus-sum').val()) || 0
let rowTotal = salary + ($tr.find('.bonus-in:checked').length ? bonus : 0);
$tr.find('.netpay').text(rowTotal.toFixed(2));
updateTotal();
}
function updateTotal() {
let total = 0;
$('.netpay').each((i, el) => total += parseFloat(el.textContent.trim() || 0));
$('#total').text('₹ ' + total.toFixed(2));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<table>
<thead>
<tr>
<th class="text-center"><input type="checkbox" checked="checked" class="checkAll" name="checkAll" /></th>
<th>#</th>
<th>Beneficiary Name</th>
<th class="text-right box">Bonus ₹</th>
<th class="text-right">Salary ₹</th>
<th class="text-right">Net pay ₹</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>1</td>
<td>Chellammal Kochimoni</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>2</td>
<td>Christal Prema G.</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>3</td>
<td>Kamalesan T.</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>4</td>
<td>Palammal A.</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
<tr>
<td>
<input type="checkbox" checked="checked" class="bonus-in" name="bene_id[]" value="" />
</td>
<td>4</td>
<td>Thangapazham</td>
<td><input type="text" class="bonus-sum" name="bonus_temp[]" value="" /></td>
<td class="wagein">400</td>
<td class="netpay"></td>
</tr>
</tbody>
</table>
</div>
<div>Total Net Pay <span id="total">₹ 0.00</span></div>
Note the removal of all the duplicate id attributes in your HTML . They are invalid, and are not required anyway.
I have introduced new variable totalsum.
Replace the below function .your code will work as expected
<script>
var totalsum=0;
$('.bonus-sum').keyup(function() {
var sum = 0;
var salary = parseFloat($(this).closest('tr').find('.wagein').text());
var bonus = parseFloat($(this).closest('tr').find('.bonus-sum').val()) || 0;
$(this).closest('tr').find('.bonus-in:checked').each(function() {
sum = salary + bonus;
totalsum+=sum;
});
$(this).closest('tr').find('.netpay').text(sum.toFixed(2));
var netpay = 0;
netpay += totalsum;
$('#total').text('₹ ' + netpay.toFixed(2));
});
</script>
How to get the numbers displayed in the display textbox when the user clicks the number buttons? I do not know what to put in my numInputF() function, I want to use a for-loop function, but I do not know how.
<script>
var initialMoney = 1000; //Customer's initial money is $1000
var display;
/* Set the "Type amount and select operation to blank." */
function setBlankF(){
document.getElementById("display").value = "";
}
/* Gets the user input when buttons are clicked. */
function numInputF(){
}
This is my body part. I know that I should put something on the onclick button, but I have not figured out how to make my function, which is why it is blank.
<table border="1">
<caption>ATM 360</caption>
<tr>
<td colspan="4">
<input type="text" id="display" value="Type amount and select operation" size="30" onclick="setBlankF()">
</td>
</tr>
<tr>
<td>
<input type="button" value="1" onclick="">
</td>
<td>
<input type="button" value="2" onclick="">
</td>
<td>
<input type="button" value="3" onclick="">
</td>
<td>
<input type="button" value="Withdrawal" id="withdraw" onclick="">
</td>
</tr>
<tr>
<td>
<input type="button" value="4" onclick="">
</td>
<td>
<input type="button" value="5" onclick="">
</td>
<td>
<input type="button" value="6" onclick="">
</td>
<td>
<input type="button" value="Deposit" id="deposit" onclick="">
</td>
</tr>
<tr>
<td>
<input type="button" value="7" onclick="">
</td>
<td>
<input type="button" value="8" onclick="">
</td>
<td>
<input type="button" value="9" onclick="">
</td>
<td>
<input type="button" value="Retype" id="retype" onclick="">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" value="0" onclick="">
</td>
<td>
</td>
<td>
<input type="button" value="End" id="end" onclick="">
</td>
</tr>
</table>
Thank you in advance.
First attach the click event to all the buttons, so get all buttons elements with type=button using .querySelectorAll() then loop through them and pick one by one buttons[i] and attach the click event using .addEventListener this click will lead us to the function numInputF as defined in the passed arguments :
var buttons = document.querySelectorAll('[type="button"]');
for(var i=0;i<buttons.length;i++){
buttons[i].addEventListener("click", numInputF, false);
}
Then in your function you could get the value like :
function numInputF(){
alert(this.value);
}
Hope this helps.
var buttons = document.querySelectorAll('[type="button"]');
for(var i=0;i<buttons.length;i++){
buttons[i].addEventListener("click", numInputF, false);
}
function numInputF(){
alert(this.value);
}
<table border="1">
<caption>ATM 360</caption>
<tr>
<td colspan="4">
<input type="text" id="display" value="Type amount and select operation" size="30" onclick="setBlankF()">
</td>
</tr>
<tr>
<td>
<input type="button" value="1" onclick="">
</td>
<td>
<input type="button" value="2" onclick="">
</td>
<td>
<input type="button" value="3" onclick="">
</td>
<td>
<input type="button" value="Withdrawal" id="withdraw" onclick="">
</td>
</tr>
<tr>
<td>
<input type="button" value="4" onclick="">
</td>
<td>
<input type="button" value="5" onclick="">
</td>
<td>
<input type="button" value="6" onclick="">
</td>
<td>
<input type="button" value="Deposit" id="deposit" onclick="">
</td>
</tr>
<tr>
<td>
<input type="button" value="7" onclick="">
</td>
<td>
<input type="button" value="8" onclick="">
</td>
<td>
<input type="button" value="9" onclick="">
</td>
<td>
<input type="button" value="Retype" id="retype" onclick="">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" value="0" onclick="">
</td>
<td>
</td>
<td>
<input type="button" value="End" id="end" onclick="">
</td>
</tr>
</table>
I am trying to create a table that when a user click update, it will get the first and second input value. I am trying to use prev to find the first two DOM element and get the value, but fail, what should I do? Any help is appreciated.
$('.update_button').on("click",function(){
var update_val_1 = $(this).prev().find('input:first').val();
var update_val_2 = $(this).prev().find('input:nth-child(2)').val();
alert(update_val_1);
alert(update_val_2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr class="gridtable">
<td><input type="text" value="apple" /></td>
<td><input type="text" value="one" /></td>
<td><button type="button" class="update_button">UPDATE</button></td>
</tr>
<tr class="gridtable">
<td><input type="text" value="banana" /></td>
<td><input type="text" value="three" /></td>
<td><button type="button" class="update_button">UPDATE</button></td>
</tr>
<tr class="gridtable">
<td><input type="text" value="berry" /></td>
<td><input type="text" value="ten" /></td>
<td><button type="button" class="update_button">UPDATE</button></td>
</tr>
The problem is that prev looks for sibling nodes. Your input elements are in different td's so they aren't siblings. Instead, you need to go to the parent row then grab the inputs from there.
Explicit example:
$('.update_button').on("click", function() {
var $parentRow = $(this).closest('.gridtable');
var $firstCell = $parentRow.find('td:first-child');
var $secondCell = $parentRow.find('td:nth-child(2)');
var $firstInput = $firstCell.find('input');
var $secondInput = $secondCell.find('input');
var update_val_1 = $firstInput.val();
var update_val_2 = $secondInput.val();
console.log(update_val_1);
console.log(update_val_2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="gridtable">
<td>
<input type="text" value="apple" />
</td>
<td>
<input type="text" value="one" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
<tr class="gridtable">
<td>
<input type="text" value="banana" />
</td>
<td>
<input type="text" value="three" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
<tr class="gridtable">
<td>
<input type="text" value="berry" />
</td>
<td>
<input type="text" value="ten" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
</table>
Simplified example:
$('.update_button').on("click", function() {
var $parentRow = $(this).closest('.gridtable');
var update_val_1 = $parentRow.find('td:first-child input').val();
var update_val_2 = $parentRow.find('td:nth-child(2) input').val();
console.log(update_val_1);
console.log(update_val_2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="gridtable">
<td>
<input type="text" value="apple" />
</td>
<td>
<input type="text" value="one" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
<tr class="gridtable">
<td>
<input type="text" value="banana" />
</td>
<td>
<input type="text" value="three" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
<tr class="gridtable">
<td>
<input type="text" value="berry" />
</td>
<td>
<input type="text" value="ten" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
</table>
I'm a big fan of the .closest(selector) and .find('selector') methods.
The first one goes up the dom until it finds the first match of the given selector.
The second one goes down the dom and finds all matches.
.eq(index) is like array[index] for jQuery. It takes a collection of jQuery elements and returns the one at the given index.
$('.update_button').on("click",function(){
var inputs = $(this).closest('.gridtable').find('input');
var value1 = inputs.eq(0).val();
var value2 = inputs.eq(1).val();
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wanted swap the element values, on UP/Down button clicks. For example in the below html snippet, I have two sets Guidance and Communication, on UP/Down button click, all the values of Guidance set(checkbox, textbox values) has to swapped with Communication values. I am planning to write up a simple javascript swap function, could you help me write it using simple and clean javascript code?
JS:
var from_list = getElementsByClassName("from");
var to_list = getElementsByClassName("to");
for (var i = 0; i < from_list.length; i++) {
//swap logic
}
HTML:
<table>
<tr>
<td></td>
<td>Guidance :</td>
<td>
<table>
<tr>
<td>
<input type="checkbox" class="guidance" name="guidlobs" value="g1">Box1</td>
<td>
<input type="checkbox" class="guidance" name="guidlobs" value="g2">Box2</td>
<td>
<input type="checkbox" class="guidance" name="guidlobs" value="g3">Box3</td>
</tr>
</table>
</td>
</tr>
<tr>
<td></td>
<td>Guid Name:</td>
<td>
<input type="text" class="guidance" name="guidance_name" style="width:500px;" value="" />
<br />
</td>
</tr>
<tr>
<td></td>
<td>Guid URL :</td>
<td>
<input type="text" class="guidance" name="guidance_url" style="width:500px;" value="" />
<br />
</td>
</tr>
<tr>
<td>
<input type="button" value="DN" onclick="swap('guidance','communication')" />
</td>
<td>Guid Description:</td>
<td>
<input type="text" class="guidance" name="guidance_desc" style="width:500px;" value="" />
<br />
</td>
</tr>
<tr>
<td></td>
<td>Communication :</td>
<td>
<table>
<tr>
<td>
<input type="checkbox" name="commlobs" value="c1">Box1</td>
<td>
<input type="checkbox" name="commlobs" value="c2">Box2</td>
<td>
<input type="checkbox" name="commlobs" value="c3">Box3</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<input type="button" value="UP" onclick="swap('communication','guidance')" />
</td>
<td>Communication Name :</td>
<td>
<input type="text" class="communication" name="communication_name" style="width:500px;" value="" />
<br />
</td>
</tr>
<tr>
<td></td>
<td>Communication URL:</td>
<td>
<input type="text" class="communication" name="communication_url" style="width:500px;" value="" />
<br />
</td>
</tr>
<tr>
<td>
<input type="button" value="DN" onclick="swap('communication','training')" />
</td>
<td>Communication Description :</td>
<td>
<input type="text" class="communication" name="communication_desc" style="width:500px;" value="" />
<br />
</td>
</tr>
</table>
It's not clear to me what you want to achieve. See if that helps you:
window.swap = function(fromClass, toClass) {
var from_list= document.getElementsByClassName(fromClass);
var to_list = document.getElementsByClassName(toClass);
var tmp;
for(var i = 0; i < from_list.length; i++) {
//swap logic
if (from_list[i].type == "checkbox") {
tmp = from_list[i].checked;
from_list[i].checked = to_list[i].checked;
to_list[i].checked = tmp;
} else {
tmp = from_list[i].value;
from_list[i].value = to_list[i].value;
to_list[i].value = tmp;
}
}
}
JSFiddle: http://jsfiddle.net/1cgeyyb0/1/
I have table like this:
<tr id="t0" >
<td id="0"></td>
<td ><input name="0" style="width:20px;" type="text" />-
<input name="1" style="width:20px;" type="text" />
<button onclick="write(0)" >+</button></td>
</tr>
<tr id="t1" >
<td id="1"></td>
<td ><input name="1" style="width:20px;" type="text" />-
<input name="2" style="width:20px;" type="text" />
<button onclick="write(1)" >+</button></td>
</tr>
I need to get all values from input elements inside specific tr element.
I tried following but nothing:
var t = $('table#tbl tr#'+id).find('input').val();
alert(t);
or
var t = $('tr'+id+':has(input)').val();
alert(t);
but nothing... Can someone point me to the correct answer?
You can your .map to get all values since .val() gives you the value of the first input only. Each of your rows has an id; since IDs are unique, that's all you need to select a particular row.
var t = $('#t0').find('input').map(function() {
return this.value;
}).get();
alert(t);
var t = $('#t0').find('input').map(function() {
return this.value;
}).get();
alert( t );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id="t0" >
<td id="0"></td>
<td ><input name="0" style="width:20px;" type="text" value="test 1" />-
<input name="1" style="width:20px;" type="text" value="test 2" />
<button onclick="write(0)" >+</button></td>
</tr>
<tr id="t1" >
<td id="1"></td>
<td ><input name="1" style="width:20px;" type="text" />-
<input name="2" style="width:20px;" type="text" />
<button onclick="write(1)" >+</button></td>
</tr>
</table>
Reference:
.map() | jQuery API Documentation