Trying to do some calc work with a dynamic table.
I have a table which uses a PHP for loop to create 2 rows with text inputs for the amount of days in the month.
I then need to take the first text input, and multiply it by 0.25 and show the result in the text input next to it.
Everything works, but cant get it to select the next text input.
This is the current:
$(".miles-input").on('keyup', function(){
var fuel_cost = $('.fuel_cost').val();
fuel_cost = parseFloat(fuel_cost);
var miles = $(this).val();
$('.price').val('£' +miles*fuel_cost);
});
The table when created created looks like:
<td><input type="text" class="miles-input"></td>
<td><input type="text" class="price"></td>
What I currently have changes all of the price inputs (of course) but no matter what I try I cannot get only the next input to change.
I have tried:
miles.next('.price').val('£' +miles*fuel_cost);
miles.closest('.price').val('£' +miles*fuel_cost);
Heres a jsFiddle:
http://jsfiddle.net/cd7t3ohn/
etc but none work. What is the correct way?
You can use:
$(this).parents("tr:first").find(".price:first").val('£' + (miles*fuel_cost));
It is not the best answer, but it will work. It will get you at the parent tr and then finding the first .price class.
miles is a String. Use this as your reference instead
$(this).next('.price').val('£' +miles*fuel_cost);
Try this:
$(function () {
$(".miles-input").on('keyup', function () {
var fuel_cost = $('.fuel_cost').val();
fuel_cost = parseFloat(fuel_cost);
var miles = $(this).val();
$(this).closest("tr").find(".price").val('£' + miles * fuel_cost);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
Fuel cost:
<input type='text' class='fuel_cost' value='0.25'>
<br>
<br>
<table style="width:500px">
<tr>
<td>Date</td>
<td>Miles</td>
<td>Cost</td>
</tr>
<tr class="input">
<td>1 / 2 / 2015</td>
<td>
<input type='text' class='miles-input' />
</td>
<td>
<input type='text' class='price' />
</td>
</tr>
<tr>
<td>1 / 2 / 2015</td>
<td>
<input type='text' class='miles-input'/>
</td>
<td>
<input type='text' class='price'/>
</td>
</tr>
</table>
my 5 cents
$(this).parent() // get <tr>
.next() // next row
.children('.price') // <td> with price
.etc...
Related
We have multiple line items on some orders and others that only have one. I only want one checkbox per similar order number. When the box is checked I need to highlight every line that has that order number. Of course the order number will always be different. What I am doing is putting the order number in the td as a value for every similar row. The order number is also a value of the checkbox input. So if I checked the 1000002 check box it would highlight both rows that same order number. I am new to jQuery and I found some jquery that highlight the closest column, but I know this will have to be changed alot to get it to do what I need it too.
<tr class="danger" value="1000005">
<td><input name="checkOrder[1000005][ord_no]" value=" 1000005" type="checkbox" class="checkOrder" id="checkOrder"></td>
<td> 1000005 </td>
<td> 65734COM </td>
<td> dummy product1</td>
<td>$565.00</td>
<td>$0 </td>
<td>$565</td></tr>
<tr class="danger" value="1000002">
<td><input name="checkOrder[1000002][ord_no]" value=" 1000002" type="checkbox" class="checkOrder" id="checkOrder"></td>
<td> 1000002 </td>
<td> 54354534 </td>
<td> dummy product2</td>
<td>$900.00</td>
<td>$1080.46 </td>
<td>$-180.46</td></tr>
<tr class="danger" value="1000002">
<td></td>
<td> 1000002</td>
<td> 087373 </td>
<td> dummy product3</td>
<td>$750.00</td>
<td>$919.00 </td>
<td>$-169</td></tr>
My jQuery
$("input[type='checkbox']").change(function(e) {
var myName = $(this).attr("value");
if($(this).is(":checked")){
if(myName == )
$(this).closest('tr').addClass("primary");
$(this).closest('tr').removeClass("danger");
}
else{
$(this).closest('tr').removeClass("primary");
$(this).closest('tr').addClass("danger");
}
});
You should be looking for the closest tr (row) , not the closest td (column).
Use $(this).closest('tr').addClass("highlight");
Also, you need to find first the rows that you need to change:
Get the ordernumber
var ordernum = $(this).closest('tr').find('td:nth-child(2)').attr('value');
Then find all rows in the table that have the same ordernumber
$('table td[value="' + ordernum + '"]').each(function(i, v) {
$(v).closest('tr').addClass('danger');
});
$(function() {
$('.checkOrder').click(function() {
var ordernum = $(this).closest('tr').find('td:nth-child(2)').attr('value');
if ($(this).is(':checked')) {
$('table td[value="' + ordernum + '"]').each(function(i, v) {
$(v).closest('tr').addClass('danger');
});
} else {
$('table td[value="' + ordernum + '"]').each(function(i, v) {
$(v).closest('tr').removeClass('danger');
});
}
});
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
<tbody>
<tr>
<td>
<input name="checkOrder[1000005][ord_no]" value=" 1000005" type="checkbox" class="checkOrder" id="checkOrder">
</td>
<td value="1000005">1000005</td>
<td>65734COM</td>
<td>dummy product1</td>
<td>$565.00</td>
<td>$0</td>
<td>$565</td>
</tr>
<tr>
<td>
<input name="checkOrder[1000002][ord_no]" value=" 1000002" type="checkbox" class="checkOrder" id="checkOrder">
</td>
<td value="1000002">1000002</td>
<td>54354534</td>
<td>dummy product2</td>
<td>$900.00</td>
<td>$1080.46</td>
<td>$-180.46</td>
</tr>
<tr>
<td value="1000002"></td>
<td>1000002</td>
<td>087373</td>
<td>dummy product3</td>
<td>$750.00</td>
<td>$919.00</td>
<td>$-169</td>
</tr>
</tbody>
</table>
So I have this snippet of code that i want to insert into my site.
<table border="1">
<col width="130">
<col width="80">
<tr>
<td align = "right">Steps:</td><td align="center">Add as many steps as you need</td>
</tr>
<tr>
<td align = "right">Step 1:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
<tr>
<td align = "right">Step 2:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
<!--<tr>
<td align = "right">Step 3:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
<tr>
<td align = "right">Step 4:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
-->
</table>
For the most part, I have some commented out since I am testing to make sure that my site can handle multiple text boxs but the problem that I have is how can I insert steps into my site using php? I have these all on .php files in case you are wondering but I would like to insert more boxes with a button that is just under the current boxes. Every time the user clicks it, it should insert another box underneath the current boxes and update the number accordingly. I just am not sure where to start and how to get them in. Any ideas?
You would create new elements in JS and jQuery using the following code:
HTML
<button id="addButton">Add me!</button>
JS
var stepCounter = 3; //Number of the first step to be added
$('#addButton').click(function () {
var tr = document.createElement("tr");
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var textarea = document.createElement("textarea");
$(textarea).attr("name", "steps[]");
$(td1).innerHTML("Step " + stepCounter);
stepCounter++;
$(tr).append(td1);
$(tr).append(td2);
$(td2).append(textarea);
$('#giveTheColAnId').append(tr);
});
Remember to include jQuery for this solution
Note that this code might not be perfect, but it should give you a very good start.
You can add these new Boxes with JavaScript/JQuery, if you dont want to reload the page after adding a new box. I would really recommend to have a look at this, instead of trying to do it with PH.
If you really want to do it just with php, you have to have a submit-button in a form
so add something like
<form method="post" action="?">
<input type="submit" name="submit" value="add row">
</form>
Now the PHP-part:
if(isset($_POST['submit'])) {
echo " [your usual tr-td-structure] ";
}
Keep in mind, that after reloading the page, a variable will lose its value.
So if you want to count up, you have to do it with javascript (or determine somehow which Step is the current and insert it in the echo). Of course set the last step in a hidden field inside the form (or append it as a get-param).
Can you use Jquery, Can't you do something like this ?
$(document).ready(function(){
$('#myButton').click(function(){
$(':input').append('<input type="textbox">');
});
});
I'm trying to create a dynamic table, in other words, a table where the number of columns per row
isn't necessarily the same as the other rows
can be changed live by the user
in every added column, there is contained a dropdown box which is filled using a database request.
I would like to be able to fill this dropdown once when the page loads, and not every time a column is added(it's for a calendar, so worst case: multiple times per row = 31*x).
I'm using jquery 1.9 and php 5.3 for my programming.
I've tried with JSON and $.post(), but this escapes too much of the needed slashes and quotes, and due to my version 5.3 of php, I can't use the "DO_NOT_ESCAPE_ANYTHING" constants provided in php 5.4+ (and yes, I know that that name isn't right, it's by heart)
So, heart of the question:
How do I use a combination of jquery and php to put a HTML -tag in a javascript variable to output on a jquery-handled button-click.
jsfiddle: http://jsfiddle.net/pjc3y/3/
code:
HTML:
<form name="myform" id="myForm" action="#test">
<div>
<table id="persCalTable">
<tr id="DAY_0">
<td>
<input type="text" name="name" size="25" value="Enter your name here!" />
</td>
<td>
<button id="eventAdder">add event</button>
</td>
</tr>
</table>
<input type="submit" value="Submit" id="submitter" />
</div>
</form>
javascript:
function addCellToRow(row, cell) {
row.append(cell);
}
function expandCalendarTable(myObj) {
var DATA = myObj.closest('tr').attr('id').slice(4); //data is the number after DAY_
var selector = '#persCalTable #DAY_' + DATA; //selector is the table and the clicked row id
var cellHiddenField = '<input type="hidden" name="status" value="New" />';
var cellOtherData = "INSERT SELECT TAG HERE";
var cell = cellHiddenField + cellOtherData;
addCellToRow($(selector), cell); // add the cell to the row defined by the selector
eve.preventDefault(); // stop the page refresh(if not in a form, this is not needed, when in a form, this is needed)
//alert('Picked: ' + DATA);
}
$(document).ready(function () {
$("#submitter").click(
function (e) {
e.preventDefault();
alert($('#myForm').serialize());
});
$("[id^=eventAdder]").click(
function (e) {
e.preventDefault();
expandCalendarTable($(this));
});
});
While you can have differing columns per row in an HTML table, if you have the following:
<table>
<tr>
<td>Col 1</td>
<td>Col 2</td>
<td>Col 3</td>
</tr>
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
</table>
What you will end up with is a table with 3 columns, and in the second row the third cell will be empty and borderless. What you can do is use a seperate table for each row, set the widths as equal and calculate and set cell widths manually with jQuery.
As for the calendar. I would suggest you use JQueryUi, which has a great calender control or a similar library than going for a select. Either way, you can prepare the object ahead of time and store it in a variable. You will then have to use clone on it otherwise it will move to the last place you inserted it at.
$(function(){
// Prepare the select (obviously yours would be more complicated)
var select = $("<select>");
<?php for( $i = 0; $i < count($optionValue); $i++ ) { ?>
select.append($("<option value='<?php echo $optionValue[$i]?>'><?php echo $optionText[$i]?></option>"));
<?php } ?>
// Insert into relevant parts
$("#insert1").append(select.clone());
$("#insert1 select").attr("name", "select1");
$("#insert2").append(select.clone());
$("#insert2 select").attr("name", "select2");
})
I have an html table built from a database query which loops through and creates a button for each camera name found and puts them in a table:
<?php
for($i=0;$i<$num_rows;$i++)
{
?>
<tr>
<td>
<input type="submit" class="play" data-hash="<?php echo $result_cameras[$i]["camera_hash"]; ?>" value="<?php echo $result_cameras[$i]["camera_name"]; ?>">
</td>
</tr>
<?php
}
?>
This resolves to something like this:
<tr>
<td>
<input type="submit" class="play" data-hash="0d3d0ac6e54a640c73f1149d4d0bbc38e99d10f5" value="Office Window">
</td>
</tr>
<tr>
<td>
<input type="submit" class="play" data-hash="b824cba374c3d5ab7806ad8260c939323c03147b" value="aaa">
</td>
</tr>
<tr>
<td>
<input type="submit" class="play" data-hash="ec9658f0c1855e2e2ac09ae284f5e6990dbf445d" value="laptop">
</td>
</tr>
Notice the data hash attribute is different for each button. I want to process this button with my jquery code:
$(".play").click(function(){
var camerahash = $('input').data('hash');
console.log($('input').data('hash'));
});
No matter which button I click I will always get the hash from the first button I click: 0d3d0ac6e54a640c73f1149d4d0bbc38e99d10f5. Any help is appreciated.
$('input').data('hash') gives you the data attribute of the first input in the selection use $(this).data('hash') to get the data attribute of the currently clicked input
You need to specify which input element to read.
Try something like:
$(".play").click(function(){
$this = $(this);
var camerahash = $this.data('hash');
console.log($this.data('hash'));
});
You are always calling the first object of .play. This would be a correct way:
$('.play').on('click', function(){
var camerahash = $(this).data('hash');
});
You could always grab them by using the .attr(data-hash) html5 attribute.
Try:
$('.play').on('click', function() {
var _hash = $(this).attr('data-hash');
console.log(_hash);
});
Your selector will return the first one that it comes to. Use this instead
<script>
$("input.play").click(function() {
alert($(this).data('hash'));
});
</script>
The below coding is my doubt. the total fees value is fetched from db. and paid is currently we enter. now i want that if i enter any value in paid, the entered value is subtract from db value and new value is display in bal fees text box and as well as store in db.
my coding is...
<HTML>
<HEAD>
<script language="javascript">
function doMath()
{
var oldbal= parseInt(document.getElementById('totalfees').value);
var paid= parseInt(document.getElementById('paid').value);
var totalfees = oldbal-paid;
document.getElementById('totalfees').value = totalfees;
}
</script>
</HEAD>
<body>
<?php
//fteching query
include("db.php");
$billtype_id = $_GET['billtype_id'];
$result=mysql_query("select * from bill_type where billtype_id=$billtype_id");
while($res=mysql_fetch_array($result))
{
$totalfees=$res['totalfees'];
}
//inserting query
$sql=mysql_query("insert into fees_admin values totalfees='$balfees' WHERE billtype_id='$billtype_id'",$conn);
include("config.php");
$balfees=$_POST['totalfees'];
$sql=mysql_query("insert into fees_admin values totalfees='$balfees' WHERE billtype_id='$billtype_id'",$conn);
?>
//html form
<form name="form" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<table>
<tr>
<td>Total Fees :</td>
<td><input name="total" type="text" id="oldbal" value="<?php echo $totalfees;?>" readonly="true" style="background-color:#FFFFFF"></td>
</tr>
<tr>
<td>Paid :</td>
<td><input type="text" id="paid" name="paid" onKeyUp="doMath();"></td>
</tr>
<tr>
<td>Bal Fees</td>
<td><input name="totalfees" type="text" id="totalfees" readonly="true" style="background-color:#FFFFFF"></td>
</tr>
</table>
</form>
Thanks for reading. please clear my doubt..
To be clear, input elements will return a string when you call "value". So in your example you would need to change the subtraction equation to:
var totalfees = parseFloat(oldbal) - parseFloat(paid);
You can use parseFloat to create a float from a string or parseInt if you don't need decimal precision.
You should probably also change you HTML code to make the DOM level 0 event "change" rather than keyUp. Its more consistent across a range of different browsers (Desktop, Mobile, Tablet).
Change
var oldbal= parseInt(document.getElementById('totalfees').value);
to
var oldbal = parseInt(document.getElementById('oldbal').value);
Function should then look like
function doMath()
{
var oldbal = document.getElementById('oldbal').value;
var paid = document.getElementById('paid').value;
var totalfees = parseInt(oldbal) - parseInt(paid);
document.getElementById('totalfees').value = totalfees;
}
I believe you were retrieving the wrong input element.