Javascript/jquery get number value inside of element [duplicate] - javascript

This question already has answers here:
get element value using jquery
(2 answers)
Closed 9 years ago.
<span id="rate">2388</span>
var = $("#rate")...
I need get number value inside of element. any one know how to do it?

var number = +$("#rate").text();
// | \______get text of span as string
// |_________________unary plus converts to numeric

You can get the text inside your span using text() method and parseInt() to convert it to number:
var rateVal = parseInt($("#rate").text(), 10);

You can do also this
var result = Number($("#rate").html());
But becareful to use Number functon, Please have look this discussion for difference between parseInt and Number.
What is the difference between parseInt(string) and Number(string) in JavaScript?, as Roko indicated.

Related

How do I convert my HEX string into a number? [duplicate]

This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 3 years ago.
I wish to convert the whiteHex variable to a decimal using the parseInt() function, and store it in a variable, whiteDecimal.
var whiteHex = 'ffffff';
var whiteDecimal = parseInt(whiteHex);
I am unsure if the above is correct or not. The reason being, that I then wish to subtract 1 from whiteDecimal and store it in a variable offWhiteDecimal. This is where I am getting stuck. How can I subtract one from the ffffff hex value? Am I missing something within the parseInt function?
You're looking for this:
var whiteDecimal = parseInt(whiteHex, 16)
console.log(whiteDecimal - 1);
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Syntax

Multiply a string in JS [duplicate]

This question already has answers here:
Repeat a string in JavaScript a number of times
(24 answers)
Closed 4 years ago.
i want to display a string as many times I have a generated variable. Therefore I'd like to do something like that, which doesn't work
var shower_total = 7; // this gets generated, but for simplification...
var uhrzeit = "<p class='clock_item'>Foo</p>";
document.getElementById("uhrzeit_block").innerHTML =5*uhrzeit;
That's why I tried looping it but that doesn't work neither
document.getElementById("uhrzeit_block").innerHTML =
for(b=0, b <= shower_total; b++){
uhrzeit
};
What do I do wrong or what would be a possible - beginner-compatible - solution. Thanks!
You could use String#repeat instead of a multiplication of a string. This does not work for value who can not converted to a number.
var uhrzeit = "<p class='clock_item'>Foo</p>";
console.log(uhrzeit.repeat(5));

Add +1 for data attributes not working [duplicate]

This question already has answers here:
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed 5 years ago.
This's my codes:
var following_tags = $('.follow-tags-start').attr("data-following-tags");
$('.follow-tags-start').attr('data-following-tags', following_tags+1);
the issue that if the count of data-following-tags="0" after do the action the count add 1 with the exists count so each time i do the action it's add 1 with 0.
data-following-tags="01111"
you can use parseInt() (assuming these are integer numbers) to convert data attribute to integer, then you can add 1. something like this:
var following_tags = parseInt($('.follow-tags-start').attr("data-following-tags"));
$('.follow-tags-start').attr('data-following-tags', following_tags+1);
You can just use a callback, and coerce the value to a number
$('.follow-tags-start').attr("data-following-tags", function(attr) {
return (+attr) + 1
});

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

How can I convert my string to a number in Javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert String variable to int in javascript?
I have the following:
var dataElapsed = $("#stats-list").attr("data-elapsed");
This creates a string called dataElapsed but I expect a number. How can I convert this?
Is there some way to convert with JQuery? or do I have to use some Javascript way?
Try this(assuming the data attributes have valid numeric values):
var dataElapsed = parseFloat($("#stats-list").attr("data-elapsed")); //for decimal.
or
var dataElapsed = parseInt($("#stats-list").attr("data-elapsed"), 10); //for integer.
You can use the .data() method to get values from data attributes
var dataElapsed = $("#stats-list").data("elapsed");

Categories

Resources