Reputation: 1145
I have some number printed on my html for special usage, therefore i need to grab it from html, alter it in javascript and output again.
So i have few numbers like below in my HTML:
<p id="usage">6564</p>
and i'd like to have a if statment in order do some modifcation of "6564", below is some pseudo codes, which tells what i'd like to do.
var checker = $("#usage").html();
if(checker >= 2 digits){
//display the first two digits
}
else {
$("#usage").html("1");
}
The if statement should display the first two digits only, if the number has more than 2 digits otherwise it will return "10", So the result should be "65"
Solution :
For people who experience the same issues as me
var checker = $(".totalPage").text().replace(/,/g, ""); //it you have comma between your number like 6,545
var pagevalue = parseInt(checker.slice(0,2));
if(checker.length >= 2){
$(".totalPage").text(pagevalue+1);//you can modify the our put even further
}
else {
$(".totalPage").text("10");
}
Upvotes: 1
Views: 373
Reputation: 1331
Here's the pure Js version.
function extractFunction() {
var checker = document.getElementById("usage").innerHTML;
if (checker.length > 2) { //if cheacker lentgh > 2
var result = checker.substr(0, 2); //start at char 1 and pull first 2 chars
document.getElementById("usage").innerHTML = result;
}
else{
document.getElementById("usage").innerHTML = "1";
}
}
<p id="usage">6564</p>
<button onclick="extractFunction()">Extract it</button>
Upvotes: 1
Reputation: 15555
var checker = $("#usage").text();
if(checker.length >= 2){
$("#usage").text(checker.slice(0,2));
}
else {
$("#usage").text("10");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="usage">6564</p>
Upvotes: 1