<script type="text/javascript">
$(document).ready(function() {
var sub_total = $("sub_total").text();
alert(sub_total);
var ship_total = $("ship_total").val()
alert(ship_total);
var new_total = sub_total + ship_total;
$('#new_total').html( new_total.toFixed(2));
});
</script>
I'm using alert to test the output and It is not getting the value inside the span ID.
$<span id="sub_total"><?php echo $sub_total = number_format($this->cart->total(), 2); ?></span></td>
This does display correctly on the page but the alert is a blank box. Causing the new_total value to stat NaN not a number.
How do I add the values in these two fields?
EDIT:
You forgot to get the value - you are only getting the element
Since you're already using jQuery you can get the value like
var sub_total = parseInt($("#sub_total").text());// this is where you need to use .text()
var ship_total = parseInt($("#ship_total").text());
or if you want to keep plain js..
document.getElementById("sub_total").value
Since you have $ in your text you need to get rid of it before parsing it. You can use regex as #Bubbles stated or whatever method you want to remove the $ sign.. ex. substring.. split..
$.trim($("#sub_total").text().split('$')[1])
or
$.trim($("#sub_total").text()).substring(1)
wirey gives a good review of part of the problem, but as is I don't believe it's enough. "parseInt" won't work on "$20", you have to remove the dollar sign first. If you're feeling lazy, this is nothing a good regex won't be able to solve in short order:
var sub_total = parseInt($("#sub_total").text().match(/[0-9]+/)));
var ship_total = parseInt($("#ship_total").text().match(/[0-9]+/)));
This will just grab the number from the span, then parse it.
Related
Im using jspdf to create a pdf from some divs i have.
There are a lot of inputs,some of them are required,some are not. I need to get only the values that are not empty from my input fields,but i can't make it work,even tho im 90% sure my code should work fine. Here it is:
var doc = new jsPDF();
var i=1;
$("div#home :input[type=text]").each(function(){
i++;
var input = $(this);
if(input.val().lenght>0){
console.log(input);
var text = input.attr('placeholder')+': '+input.val();
doc.setFontSize(10);
doc.text(10, (i*8) + (10),text+"\n");
}
});
doc.save('a4.pdf')
The code above does not work. when i use it,i get nothing for the pdf. if i remove the .lenght>0 it works,but it gets all the empty values so my pdf goes with a lot of blank spaces. i have tried using input.val().>0,input.val().!=='',and none of them works,they all give me the same result.
How can i check properly if my input fields are not null?
This is probably a typo:
input.val().lenght
should be this instead:
input.val().length
You should see an error message in your browser console, because length was misspelled.
I found the answer to my own problem. The code is fine,the problem was in my auto increment variable "i".
var doc = new jsPDF();
var i=1;
$("div#home :input[type=text]").each(function(){
i++;
var input = $(this);
if(input.val().lenght>0){
console.log(input);
var text = input.attr('placeholder')+': '+input.val();
doc.setFontSize(10);
doc.text(10, (i*8) + (10),text+"\n");
}
});
doc.save('a4.pdf')
As you can see above,my variable was outside the "each" loop that was looping trough the non-empty values of my input fields. The loop was fine,but as the "i++" variable was out of the loop,it was couting empty values and giving me blank spaces between the non-empty ones. So the only thing i needed to do was to move my "i" inside the loop. Yes,pretty stupdid error,but it took me hours to find out.
I would like to know if the following line is the correct way to take html and put it into a Javascript var as numeric value?
var thePoolHeatingFeeRounded = Number(document.getElementsById("priceDisplayPoolHeating").innerHTML);
Nevermind that the variable name has the word 'rounded' in it. The value that I am trying to put into this var is a 2 point float that has already been rounded and it exist as html.
Below is the line of HTML code that is referenced by getElementsById...
$<div id="priceDisplayPoolHeating" class="priceDisplay">8.00</div>
Any input is greatly appreciated.
Try this instead:
var thePoolHeatingFeeRounded = Number(document.getElementById("priceDisplayPoolHeating").innerHTML);
You were calling getElementsById which is not correct (it is not plural), I changed it to getElementById
Tip: if you need to check whether the Number is valid you can use !isNaN(thePoolHeatingFeeRounded) or use a trick to turn it into a default number (such as 0 or -1) like this:
var thePoolHeatingFeeRounded = Number(document.getElementById("priceDisplayPoolHeating").innerHTML) || 0;
You can also use parseFloat():
var thePoolHeatingFeeRounded = parseFloat(document.getElementById("priceDisplayPoolHeating").innerHTML) || 0;
You are very close. Supposed to be getElementById, not Elements. I created a little code to show you how it works.
Here's what the code looks like in this website's code displayer:
function displayText(){
var thePoolHeatingFeeRounded = Number(document.getElementById("priceDisplayPoolHeating").innerHTML)
alert(thePoolHeatingFeeRounded);
}
<div id="priceDisplayPoolHeating">8.01</div><input type="button" onclick="displayText()" value="Display the innerHTML of Pool Heating">
I have two sliding bars and I want to get the value the user sets them to and do some maths on it. var days and var total are my attempts to do this. I was taking it that the UI slider stores its values as stings and not integers. But both my attempts below, then use either just return a NaN value? So what am I doing wrong?
var days = parseInt($(this).attr('#day_output'), 10);
var total = parseInt($('#amount_outputTotal'));
var fees = 5;
var valueout;
valueout = 60 * days + fees;
$(document).ready(function() {
$('#Output').html(valueout);
});
Many Thanks Glenn.
Why would a slider, which is a representation of numbers, store the value as strings? You can get the value (or values, if two handles) with the following code
var val_single = $('#your_slider').slider('value');
var val_array = $('#your_slider').slider('values');
Please rtfm http://jqueryui.com/demos/slider/
One of our projects using jqueryUI ahs following code:
fixed = $().fixFloat(ui.value);
I understand that this might don't suit your needs.
Try to change from parseInt to parseFloat and also output via console:
$(this).attr('#day_output')
Why here is nothing like .attr or .val or something?
$('#amount_outputTotal')
I have 3 HTML form inputs fields that is dynamically generated by a "add more" button, with naming for the fields name as fieldName, fieldName1, fieldName2, fieldName3, and so on.
Now, I'm trying to retrieve the value from this fields with JavaScript, using the script below.
var bookingForm = document.forms['formName'];
var qty = bookingForm.fieldName +'i'.value;
with the 'i' been a generated numeric number by a for loop
when I use alert(qty), it returns NaN, when I'm expecting the value for fieldName1, fieldName2, and so on.
But when I use;
var qty = bookingForm.fieldName.value
I can get the value in that field but get NaN when I try to concatenate 1,2,3, with the fieldName.
Any help will be very much appreciated.
You use brackets to access a property using a string:
var qty = bookingForm['fieldName' + i].value;
You can't use code like:
var qty = bookingForm.fieldName +'i'.value;
bookingForm.fieldName +'i' is a string. You have to change that string into a DOM element in order to access the .value parameter.
Try document.getElementsByName('fieldName'+i)[0].value
It's possible I'm just misusing .filter() here, but I've been trying to extract a particular piece of text from javascript option boxes without much success.
In short, each of my option boxes has a bunch of info, then, in brackets, ([x] items left in stock).
I'd like to extract that [x], but I can't seem to get any sort of regular expression filtering working in jQuery - or, perhaps, I'm doing it wrong.
In short, if anyone could complete the below, I'd be very appreciative:
$('.productSelect').change(function() {
selectedItem = $(this + 'option:selected').text();
# Now grab the integer "x" inside the ([x] items left in stock) part of the text
[ your code here]
});
Any help very appreciated.
Something along these lines:
$('.productSelect').change(function() {
selectedItem = $(this + 'option:selected').text();
# Now grab the integer "x" inside the ([x] items left in stock) part of the text
var n = selectedItem.match(/(\()(\d+)(\))/)[2];
});
Of course, this expression depends on the fact that your data is formatted as (number) string
This should get you what you need.
var pattern=/\[(\d+)\] items left in stock/;
var qty = pattern.exec(selectedItem)[1];
So you should use the JavaScript string.match function
var s = "[100] items left in stock";
var p = s.match(regexp);