I have a comma-separated number in my table. But my js on click gives only the values before the comma instead of the whole number.
<td class="amt">1,000,000.50</td>
Below is my jQuery that alerts 1.00 instead of the whole value:
$(".amt").click(function(){
var t = parseFloat($(this).text()).toFixed(2);
alert(t);
})
This code alerts the exact figure but how can I get it as a number?
$(".amt").click(function(){
var t = $(this).text();
alert(t);
})
You can use regex to remove comma and parseFloat as
parseFloat($(this).text().replace(/,/g,'')
$(".amt").click(function(){
var t = parseFloat($(this).text().replace(/,/g,''));
alert(t)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td class="amt">1,000,000.50</td>
</tr>
</table>
You are close, but before you try to convert the number string into a float using parseFloat, first you must remove all the commas from the string. You can do that using
textString.replace(/,/g, '');
Working Example:
$(".amt").click(function(){
var textString = $(this).text();
var numString = textString.replace(/,/g, '');
var numAsFloat = parseFloat(numString ).toFixed(2);
console.log("Float value = "+numAsFloat);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="amt">1,000,000.50</div>
Related
I have a table with different inputs. Using a button, I want to clone the last row of the table, and add it to the table. Furthermore, I want to replace the "index" in the name with the variable of the index.
How can I replace the string "index" with the variable index?
In this example I just show one input, because it is sufficient to get a clue.
<table class='taskTable'>
<tr>
<td>
<input name='data[index][description]'></input>
<input name='data[index][text]'></input>
<input name='data[index][date]'></input>
</td>
</tr>
</table>
<button type='button' class='addTask'>Add Row</button>
<script>
var index = 1;
$(".addTask").on('click', function(){
index = index+1;
var lastTr = $('.taskTable').find('tr:last').clone();
//replace now the string index of the name with the variable
$('.taskTable').append(lastTr);
});
</script>
Here is a link to the jsFiddle
https://jsfiddle.net/tg53c96m/
Set the name with .attr("name", value):
var newName = `data[${index}][description]`;
lastTr.find("input").attr("name", newName);
Or if you want to search/replace:
lastTr.find("input").attr("name",
(i, oldName) => oldName.replace(/\[.*\]\[/, `[${index}][`));
var index = 1;
$(".addTask").on('click', function() {
index = index + 1;
var lastTr = $('.taskTable').find('tr:last').clone();
lastTr.find("input").attr("name",
(i, oldName) => oldName.replace(/\[.*\]\[.*\]$/, `[${index}][description]`));
console.log("New name: ", lastTr.find("input").attr("name"));
$('.taskTable').append(lastTr);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class='taskTable'>
<tr>
<td>
<input name='data[index][description]'>
</td>
</tr>
</table>
<button class='addTask'>Add Row</button>
Update
Just saw the edited HTML so here's the added function to handle multiple inputs with different names:
lastTr.find('input').each(function() {
let nameText = $(this).attr('name');
let newName = nameText.split('index').join(rows);
$(this).attr('name', newName);
});
In order to change the pattern of each name without having to remember which one is which is to handle only the portion of the names that actually changes:
let newName = nameText.split('index').join(rows);
nameText is the current input name which is s String value of either:
'data[index][description]',
'data[index][text]',
/* OR */
'data[index][date]'
When .split() into an Array of Strings, it removes the text: "index":
nameText.split('index')
// Returns ['data[', '][`description or text or date`]']
Finally .join() will convert the Array of Strings into a String with the variable index number between them:
.join(index)
// Returns `'data['+index+'][`description or text or date`]'`
Add the following statement as the first line of the function:
let rows = $('.taskTable tr').length;
This will be the number of rows already in the table.
Add this after the row has been cloned but before it is appended:
lastTr.find('input').attr('name', 'data' + rows);
This finds the input nested within the new row and assign it name="data'+rows. I'm not sure what's with the [description] part of the name. Is it dynamic or literal like the [index] part. It sounds like something isn't quite right about those names...
Note: The rest of the name can be whatever, I just feel wrong to have a name looking like that. The contacted string will look like the following with the original text:
lastTr.find('input').attr('name', 'data[' + rows + '][description]');
Also, I added another function that will change all row input with the new naming pattern.
Demo
/*
This will change all row input names from
'data[index][description]'
'data[index][text]'
'data[index][date]'
to
'data['+index+'][description]'
'data['+index+'][text]'
'data['+index+'][date]'
index = 0,...N
*/
$('.addTask tr').each(function(index) {
$(this).find('input').each(function() {
let nameText = $(this).attr('name');
let newName = nameText.split('index').join(index);
$(this).attr('name', newName);
});
});
$(".addTask").on('click', function(index) {
let rows = $('.taskTable tr').length;
const lastTr = $('.taskTable').find('tr:last').clone();
lastTr.find('input').each(function() {
let nameText = $(this).attr('name');
let newName = nameText.split('index').join(rows);
$(this).attr('name', newName);
});
$('.taskTable').append(lastTr);
});
<table class='taskTable'>
<tr><td>
<input name='data[index][description]'>
<input name='data[index][text]'>
<input name='data[index][date]'>
</td></tr>
<tr><td>
<input name='data[index][description]'>
<input name='data[index][text]'>
<input name='data[index][date]'>
</td></tr>
</table>
<button class='addTask' type='button'>Add Row</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Need to remove tags from within an input's value tag.
The element is being generated by the host's CMS so the only way I can remove the bold tags is through running a script.
<span id="kkkje30">
<input type="text" id="siF14" class="manFlPassw" value="<b>generated text</b>" maxlength="15">
</span>
You can use regex to remove tag in input value like this
$(document).ready(function(){
$('input').each(function(index, item){
var regex = /(<([^>]+)>)/ig;
var value = $(item).val();
var removedtag = value.replace(regex, '');
$(this).val(removedtag);
});
})
Demo:
$(document).ready(function(){
$('input').each(function(index, item){
var regex = /(<([^>]+)>)/ig;
var value = $(item).val();
var removedtag = value.replace(regex, '');
$(this).val(removedtag);
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="kkkje30">
<input type="text" id="siF14" class="manFlPassw" value="<b>generated text</b>" maxlength="15">
</span>
Is it possible to get textarea row count in html using angularjs?
Below is javascript way to get the row count.
console.log(document.getElementById('field_id').rows)
The code below doesn't work but something similar to it, just using front-end?
{{mainCtrl.header.modelName.rows}}
Below are two types of textarea row count you can fetch using angularjs :
Fixed rows - that are pre defined for the textarea element
Dynamic rows - that can change as per the new lines added to the textarea element.
angular.module("app", []).controller("ctrl", function($scope) {
$scope.fixedRows = angular.element(document.querySelector("#txt-area")).attr("rows");
$scope.getDynamicRows = function() {
var ele = angular.element(document.querySelector("#txt-area"));
return ele.val().split(/\r|\r\n|\n/).length;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<body ng-app="app" ng-controller="ctrl">
<textarea id="txt-area" rows="10" cols="50" type="text" ng-model="textValue"></textarea>
<p>Fixed Rows Count = {{fixedRows}}, Dynamic Rows Count = {{getDynamicRows()}}
</p>
</body>
I am working in a Google Sheets file with a tab named 'POHistory' that includes a log of all order details placed by customers of our company. Each log entry can have one, or more, rows/lines depending upon how many different SKU's were ordered on that particular order. Each line/entry does include Order No., Vendor, date of order, and order total. Because each entry can repeat PO No (if multiple SKU's were ordered on the order), I want to list out (in table format) an overview of order history for our company that allows a user to quickly get a visual on a listing of all orders placed in an HTML pop-up, without SKU details. Therefore, I know I need to create a loop that grabs only the details I want for the table (order no., vendor, date, order total), but am unsure where to begin. I have done this before with select option lists, but not with tables. Thanks for your help.
APPS SCRIPT:
function htmlOrders() {
var active = SpreadsheetApp.getActive();
var sheet = active.getSheetByName("POHistory");
var lastRow = sheet.getLastRow();
var myRange = sheet.getRange("A2:A" + lastRow);
var data = myRange.getValues();
var optionsHTML = "";
for (var i = 0; i < data.length; i+=1) {
optionsHTML += '<td>' + data[i][0] + '</td>';
};
return optionsHTML;
}
HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<tr>
<th>PO Number</th>
<th>Vendor</th>
<th>Date</th>
<th>Total</th>
</tr>
<td>
< ?!= htmlOrders(); ?>
</td>
</table>
</body>
</html>
You can put something like this into your html:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function(){
google.script.run
.withSuccessHandler(displayData)
.htmlOrders();
});
function displayData(hl){
document.getElementById('puthere').innerHTML=hl;
}
console.log('MyCode');
</script>
</head>
<body>
<div id="puthere"></div>
</body>
</html>
And add orderHistory() to htmlOrders() in the server script and then insert it all at one time into a div clientside.
I am creating an HTML Invoice. The system we use outputs the data to html in HTML. I put the relevant tag (such as {{order_purchasedate}}) and the values are displayed there instead.
I need the invoice to display the total order weight for all items in the order. The weights will be displayed as numbers in a list for example "60 80 120" with just a space separating them.
How can I add the numbers is html and only display the total?
I know I could use Java but I am not familiar with this. If someone could help that would be great.
Ben
*edit I have made an attempt at the code. I am struggling getting the output. I am sure you guys must think me an idiot!
<!DOCTYPE html>
<html>
<body>
<h1>Invoice</h1>
<p id="order_weight"></p>
<script>
getTotal = function(values){
var total = values.split(" ").reduce(function(a,b){
a = parseInt(a)
b = parseInt(b)
return a+b
});
return total
}
document.getElementById("order_weight").innerHTML = getTotal(a+b);
</script>
</body>
</html>
You can use JavaScript to take your values and calculate total value.
getTotal = function(values){
var total = values.split(" ").reduce(function(a,b){
a = parseInt(a)
b = parseInt(b)
return a+b
});
return total
}
and then just call that function
getTotal(x)
where x is your list
Here is the working example:
http://jsfiddle.net/o89f1mj5/1/
<body>
<h1>Invoice</h1>
<p id="order_weight"></p>
<input id="total" name="total" type="text"></input>
<input id="totalBtn" type="button" value="Get Total" onClick="getTotal();"></input>
</body>
<script>
function getTotal() {
var values = $('#total').val();
alert(values);
var total = values.split(" ").reduce(function(a,b){
var a = parseInt(a);
var b = parseInt(b);
return a+b;
});
document.getElementById('order_weight').innerHTML = "Value: " + total;
}
</script>
Working JS Fiddle: http://jsfiddle.net/o89f1mj5/9/
In the JSFiddle put "5 15 30" into the text box (without the quote of course) and it will make the paragraph be 50
This is what #mgibala did above using a Textbox and a Button. His answer should be accepted I am just giving you it in application with a variable input option.