Scraping a float value from the DOM [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following element on my page:
<div id="price">$8.00</div>
I want to be able to assign the value 8.00 to a JavaScript variable so that I can perform math on it (specifically 8.00 * .2), and then want to take the new number and place it next to the original, while applying a line-through on the original number ONLY. So the outcome would look something like this:
$8.00 $1.60 (where the "$8.00" has a text-decoration:line-through applied to it.)
I have been able to figure out the math and resetting the text, but I need help figuring out how to take something that's displayed, and make assign its value to a variable. I am using jQuery. Any help is appreciated.

$('#price').text() will get you the text. From there, you can use parseFloat() to get the number. However, you have a $ in there, so you will need substring as well.
var price = parseFloat($('#price').text().trim().substr(1));

You can either use regex to find just the number and use that, or you can get all the information and parseFloat, which will return just the number... Or, if you can modify your html, you can do
<div id="price">$<span id="actual_price">8.00</span></div>

parseFloat($('#price').text()) will give you the number as a floating point number.
however if you want to get a variable you must use var to assign one.
Also, you will need to use substring to just get the 8.00 or the variable will be NaN.

HTML
<div id="price">$8.00</div>
jQuery
var price = $('#price').text().substring(1);
newPrice = price * 0.2;
newText = '<span>$' + price + '</span> $' + newPrice;
$('#price').html(newText);
CSS
#price span { text-decoration: line-through; }

Related

Javascript parseFloat() change number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a strange problem with JavaScript .
I have text value in a textbox '25000000' when i am
using parsefloat(txt.value), the returned value is 25000 !??
Why is 25000000 changed to 25000 ?
I think there is an alphabet somewhere in your numbers. Check the example below.There is an error in your code.
parseFloat(" 250000000 ") = 250000000
parseFloat("2018#geeksforgeeks") = 2018
parseFloat("geeksforgeeks#2018") = NaN
Thanks for all Responses.
The problem was for ',' in price.
Of course we replace ',' with '' ,but replace method
only change first one like "25,000,000,000"=>"25000,000,000".
I used str.split(',').join('') and problem solve and parsefloat returned correctly.
regards
ali

Need to divide an input by 52 using javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
My developer is currently on annual leave and I need to work out how to make a change to some code.
The form input is a salary and I am able to output this salary using:
$('#demo').html($('#salary').val());
This displays within <div id="demo"></div>
I need to display the salary input as weekly (salary / 52).
Any assistance on how this can be achieved is appreciated.
I believed it to be:
$('#demo').html($('#salary'/52).val());
But this does not work.
In your example :
$('#demo').html($('#salary'/52).val());
you are trying to add the division to the jQuery selector. that won't work.
you will need to make sure the result of the selector is a number and then divide that by 52:
$('#demo').html(parseFloat($('#salary').val())/52);
$('#demo').html($('#salary').val()/52);
I recommend you read the jQuery Documentation. You first should get value with:
$('#salary').val(); //get value
After divide:
$('#salary').val() / 52
It is a simple programming logic. After you are able to set your value in any place, like:
$('#demo').html($('#salary').val()/52);
you are trying to add the division to the jQuery selector. that's Wrong.
$('#demo').html($('#salary'/52).val());
You need to make Sure first you get value of Salary then divide it into week. and it will return as string, so you have to make to Integer OR Float
Float :
$('#demo').html(parseFloat($('#salary').val())/52);
Integer :
$('#demo').html(parseInt($('#salary').val())/52);

EmberJs Calculate the total amount [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm beginner in Emberjs,
I want to calculate the total amount in the added item list.
For example:
(03) items added in list
------------------------
Item-1 3,000.00
Item-2 4,000.00
Item-3 3,000.00
-------------------------
Total 10,000.00 Print
-------------------------
How to create the view and controller in emberjs for calculate the total amount?
How to create the PDF file and print the same output as PDF format?
Thanks advance. :)
The aggregation methods in Ember.Enumerables are handy for this type of thing. I've created a jsBin demonstrating how to use reduce() in this case. You can create a computed property for the total on your controller that sums the values of each record in your model. Note that passing 0 as the second parameter (initialValue) will seed reduce() with an integer and help you avoid the string concatenation issues you were seeing.
total: function () {
return this.get('model').reduce(function (previousValue, item, index, enumerable) {
return previousValue + item.value;
}, 0);
}.property('model.#each')
(Your second question is unrelated, but you might want to take a look at jsPDF.)

jQuery .click +..+ notation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to understand a codrops tutorial. Its essentially a slider with a thumb scroller.
I reached a point in the code where they were setting variables to represent clicked items in the thumb scroller like this.
var $currentTitle = $pg_title.find('h1:nth-child('+(current+1)+')');
var $nextTitle = $pg_title.find('h1:nth-child('+(idx+1)+')');
var $currentThumb = $pg_preview.find('img.pg_thumb:eq('+current+')');
I have never seen notation like this +....+. I have been digging and found examples where people used it in stack like this but I haven't seen anyone explain it could someone explain how +...+ returns the value of the clicked item?
When used with a string operand, it concatenates the two strings. In this case, to create a selector. When used with a number, it works as an addition operator. It is used in both ways here. So, if current == 1,
h1:nth-child('+(current+1)+')' will evaluate first to h1:nth-child('+2+'), which will ultimately evaluate to h1:nth-child(2)
'+' Use for string concatenation
$pg_title.find('h1:nth-child('+(current+1)+')');
Like:
var b = 'def';
If you want to add some other string in 'b' variable then you can use '+' for concatenation
var addSomotherSting ='abc' + b + 'ghi';
alert(addSomotherSting);
Then browser show a alert box with 'addSomotherSting' the out put is : 'abcdefghi'

can't get text highlighted when multiple tags are present [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
So I am trying to highlight text by selecting it and clicking the button (labeled get highlighted text).
This is the implementation of it:
http://jsbin.com/uzILUro/1
Part of the html that has multiple tags is not getting selected.
i.e. try to select the below paragraph and highlight it by clicking the button.
"The remainder is r when p is divided by k" means p = kq + r; the integer q is called the quotient. For instance, “The remainder is 1 when 7 is divided by 3” means 7 = 3·2 + 1. Dividing both sides of p = kq + r by k gives the following alternative form p/k = q + r/k.
All the other works except the paragraph above.
You could use window.find() in most browsers, although it may end up being removed from browsers in the future (Mozilla, WebKit). Here's an answer about that:
https://stackoverflow.com/a/5887719/96100
The main problem is that you're using var searchText = document.getSelection().toString() which doesn't take into account opening and closing tags inside your selection.
Instead you should try using searchText = newNode.innerHTML
Make these changes in your getSelectedText function, and you should be golden.
Some extra changes might be required for IE, though.

Categories