Why isn't this meter working? - javascript

So I have this code (Fiddle):
(function() {
var meter = document.getElementById('somemeter');
var Likes = document.getElementById('likes');
var Dislikes = document.getElementById('dislikes');
meter.max = Likes.innerHTML + Dislikes.innerHTML;
meter.value = Likes.innerHTML - Dislikes.innerHTML;
})();
<div id="likes">500</div>
<div id="dislikes">50</div>
<meter value="10" min="0" max="1000" id="somemeter"></meter>
As you can see, Likes.innerHTMLis 500 , and Dislikes.innerHTML is 50
For some reason, the meter.max doesn't work when I use Likes.innerHTML or Dislikes.innerHTML; however, when I substitute those in the meter.max for 500 and 50, it does work.
Am I doing something wrong?

Your code is taking the innerHTML as string. Use parseInt instead:
(function() {
var meter = document.getElementById('somemeter');
var Likes = document.getElementById('likes');
var Dislikes = document.getElementById('dislikes');
meter.max = parseInt(Likes.innerHTML, 10) + parseInt(Dislikes.innerHTML, 10);
meter.value = parseInt(Likes.innerHTML, 10) - parseInt(Dislikes.innerHTML, 10);
})();
<div id="likes">500</div>
<div id="dislikes">50</div>
<meter value="10" min="0" max="1000" id="somemeter"></meter>

Javascript detect string, you should use parseInt values, try this code:
(function() {
var meter = document.getElementById('somemeter');
var Likes = document.getElementById('likes');
var Dislikes = document.getElementById('dislikes');
meter.max = parseInt(Likes.innerHTML) + parseInt(Dislikes.innerHTML);
meter.value = parseInt(Likes.innerHTML) - parseInt(Dislikes.innerHTML);
console.log(meter.max)
})();
<div id="likes">500</div>
<div id="dislikes">50</div>
<meter value="10" min="0" max="1000" id="somemeter"></meter>

You need to convert the strings to numeric values for the math to work:
(function() {
var meter = document.getElementById('somemeter');
var Likes = document.getElementById('likes');
var Dislikes = document.getElementById('dislikes');
meter.max = Number(Likes.innerHTML) + Number(Dislikes.innerHTML);
meter.value = Number(Likes.innerHTML) - Number(Dislikes.innerHTML);
})();
Fiddle

Related

Jquery / javascript error to get live result in my project

Currently I am working in a Bitcoin live price project with jquery. Now I need to develop a live price difference percentage change calculator. Calculator is working fine. But not working automatically when Bitcoin live price changing. I need to edit in input. Keyup event needed for working. I need to make it as always automatically. Please make it as automatically without keyup.. I created a Codepen page for it. https://codepen.io/toolsim/pen/Rwywjap . My codes;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<input type="text" id="btc-price" class="main01 dz-none from01 for-copy01 input bldo te-cen" value="">
<input type="text" value="25000" class="main02 percentagez to01 input bldo te-cen">
<input type="text" class="rz-result result01 input bldo te-cen">
<script type="text/javascript">
$(document).on("change keyup blur", ".main02", function() {
var first = Number($('.from01').val());
var second = Number($('.to01').val());
var minus = second - first; // 2000 - 1000 = {1000 = minus}
var divide = (minus / first); // 1000 / 1000 = 1
var multiply = divide * 100; // 1 * 100 = 100%
$('.result01').val(Number(multiply).toFixed(2));
});
</script>
<script type="text/javascript">
let weburl = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt#trade');
let stockPriceInput = document.querySelector('#btc-price');
let lastPrice = null;
weburl.onmessage = (event) => {
let stockObject = JSON.parse(event.data);
let price = parseFloat(stockObject.p).toFixed(2);
stockPriceInput.style.color = !lastPrice || lastPrice === price ? 'black' : price > lastPrice ? 'green' : 'red';
stockPriceInput.value = price;
lastPrice = price;
};
</script>
maybe like this:
function set_bts(_price){
$('.main01').val(_price);
var first = Number($('.from01').val());
var second = Number($('.to01').val());
var minus = second - first; // 2000 - 1000 = {1000 = minus}
var divide = (minus / first); // 1000 / 1000 = 1
var multiply = divide * 100; // 1 * 100 = 100%
$('.result01').val(Number(multiply).toFixed(2));
}
var weburl = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt#trade');
weburl.onmessage = function(event){
var stockObject = JSON.parse(event.data);
var price = parseFloat(stockObject.p).toFixed(2);
set_bts(price)
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<input type="text" id="btc-price" class="main01 from01" value="">
<input type="text" value="25000" class="to01">
<input type="text" class="result01">

How to change the number on the slider in real time through javascript or CSS?

I would like to ask about the function that I made a slider that can display the value!
But I want to be able to swipe, and the numbers can change instantly, instead of stopping to display the numbers, how should this be rewritten?
I really need everyone's help, thank you in advance.
var slider = document.querySelector('#slider');
var result = document.querySelector('#result');
slider.addEventListener('change', function(event) {
var sliderValue = event.target.value;
var maxValue = 2000;
var coef = 18;
var calc;
calc = maxValue - (sliderValue * coef);
result.value = calc;
});
<input id="slider" type="range" name="points" min="0" max="100" value=0>
<input id="result" type="text" name="result">
Change your event listener from change to input.
var slider = document.querySelector('#slider');
var result = document.querySelector('#result');
slider.addEventListener('input', function(event) { //changed to input here
var sliderValue = event.target.value;
var maxValue = 2000;
var coef = 18;
var calc;
calc = maxValue - (sliderValue * coef);
result.value = calc;
});
<input id="slider" type="range" name="points" min="0" max="100" value=0>
<input id="result" type="text" name="result">

Why is my form outputting NaN?

So I have this form that is performing very basic calculations and when I submit it, it results to NaN.
The thing that is confusing me is when I do a typeof of one of the variables assigned to the value of each input, it returns "number", and yet I get NaN as a result. Can anyone tell me why or what it is I am doing wrong?
Here's my HTML:
<form name="baddiesCost">
<input type="number" placeholder="Total Caught" name="goomba" id="goomba-form">
<input type="number" placeholder="Total Caught" name="bobombs" id="bob-ombs-form">
<input type="number" placeholder="Total Caught" name="cheepCheeps" id="cheep-form">
<button id="submitForm">Submit</button>
</form>
<h1 id="total"></h1>
Here's my JavaScript:
document.baddiesCost.addEventListener("submit", function(e) {
e.preventDefault();
var goombaCaught = document.baddiesCost.goomba.value * goombaCost;
var bobombsCaught = document.baddiesCost.bobombs.value * bobombsCost;
var cheepsCaught = document.baddiesCost.cheepCheeps.value * cheepCost;
var goombaCost = 5;
var bobombsCost = 7;
var cheepCost = 11;
var showTotal = document.getElementById("total");
var total = goombaCaught + bobombsCaught + cheepsCaught;
showTotal.textContent = cheepsCaught;
console.log(bobombsCaught);
console.log(typeof bobombsCaught);
})
This statement document.baddiesCost.bobombs.value * bobombsCost; uses variable bobombsCost which is not defined at this time.
So, it is similar to: document.baddiesCost.bobombs.value * undefined; which will be NaN.
To solve this put variable inicialization before usage like in following code:
document.baddiesCost.addEventListener("submit", function(e) {
e.preventDefault();
var goombaCost = 5;
var bobombsCost = 7;
var cheepCost = 11;
var goombaCaught = document.baddiesCost.goomba.value * goombaCost;
var bobombsCaught = document.baddiesCost.bobombs.value * bobombsCost;
var cheepsCaught = document.baddiesCost.cheepCheeps.value * cheepCost;
var showTotal = document.getElementById("total");
var total = goombaCaught + bobombsCaught + cheepsCaught;
showTotal.textContent = cheepsCaught;
console.log(bobombsCaught);
console.log(typeof bobombsCaught);
})
Anyway, NaN is number type, you can refer to following post for more explaination.

Getting Cm to ft values and displaying in other element gives reference error

I have the conversion math correct(looked it up here), but getting the value from the element that displays the height in cm, then parse it to ft/inch and display it on(on click) the right hand span does not work, i get a reference error(converter not defined).
I cannot figure out why it is undefined, is it because of hoisting or can the parseInt function not have the parameters as they are?
Here is the function
var displayInches = document.getElementById("heightInches");
displayInches.addEventListener("click", function() {
toFeet(converter);
});
function toFeet(converter) {
var heightOutputCM = document.getElementById("yourHeight");
var converter = parseInt(heightOutputCM.value);
var realFeet = converter * 0.3937 / 12;
var feet = Math.floor(realFeet);
var inches = Math.round((realFeet - feet) * 12);
return feet + "and" + inches;
}
Here is the link:
https://codepen.io/damianocel/pen/ZyRogX
HTML
<h1>Alcohol blood level calculator</h1>
<fieldset>
<legend>Your Indicators</legend><br>
<label for="height" class="margin">Height:</label>
<span class="leftlabel" id=""><span id="yourHeight"></span>Cm</span>
<input type="range" id="UserInputHeight" name="height" min="0" max="200" step="1" style="width: 200px">
<span id="heightInchesSpan" class="rightlabel"><span id="heightInches"></span>Ft</span>
<br>
<label for="" class="margin">Gender:</label>
<span class="leftlabel">Male</span>
<input type="range" id="salary" name="salary" min="0" max="1" style="width: 200px">
<span class="rightlabel">Female</span>
</fieldset>
JS
// get and display height
var displayHeightInput = document.getElementById("UserInputHeight");
displayHeightInput.addEventListener("input", function() {
sliderChange(this.value);
});
function sliderChange(val) {
var heightOutput = document.getElementById("yourHeight");
heightOutput.innerHTML = val;
toFeet();
return val;
}
function toFeet() {
var heightOutputCM = document.getElementById("yourHeight");
var converter = parseInt(heightOutputCM.innerHTML);
var realFeet = converter * 0.3937 / 12;
var feet = Math.floor(realFeet);
var inches = Math.round((realFeet - feet) * 12);
document.getElementById("heightInches").innerHTML=feet + "and" + inches;
return feet + " and " + inches;
}

JavaScript If/Else Function For Input Slider

I'm working on a an input slider which uses css translate for both "-" and "+" values using a JavaScript if/else function. I'm having a little problem with the if/else statement though.
Here's a fiddle http://jsfiddle.net/cn6St/ and here's the code:
HTML
<div class="square"></div>
<span class="result-X">Transform-X:</span>
<input type="range" id="sliderX" min="0" max="100" value="50" onchange="rangevalueX.value=value"/>
<output id="rangevalueX">50</output>
JS:
var sliderx = document.getElementById("sliderX");
var box = document.querySelector('.square');
var rangevalueX = document.getElementById("rangevalueX");
sliderx.onchange = function(){
if (rangevalueX >'50') {
box.style.webkitTransform = "translateX(" + (sliderx.value) + 'px)';
rangevalueX.value = sliderx.value;
}
else if (rangevalueX <'50') {
box.style.webkitTransform = "translateX(-" + (sliderx.value) + 'px)';
rangevalueX.value = sliderx.value;
}
};
The idea is to use the value of "50" on the slider as "0" in terms of translate. If the slider value is > 50 the CSS should transform a positive value. If it's less <50 it should be negative.
Not sure what I'm doing wrong here through.
You'll want to do:
var rangevalueX = document.getElementById('rangevalueX').value;
To get the actual slider value.
I Seem to have found the issue. It was the HMTL. The value of the slider should be set to "0", while the min should be "-50" and the max value should be "50".
Final working code:
HTML:
<div class="square"></div>
<div>
<span class="result-X">Transform-X:</span>
<input type="range" id="sliderX" min="-50" max="50" value="0" onchange="rangevalueX.value=value"/>
<output id="rangevalueX">0</output>
</div>
</div>
JavaScript:
var sliderx = document.getElementById("sliderX");
var box = document.querySelector('.square');
var rangevalueX = document.getElementById("rangevalueX");
sliderx.onchange = function(){
if (rangevalueX >'50') {
box.style.webkitTransform = "translateX(" + (sliderx.value) + 'px)';
rangevalueX.value = sliderx.value;
}
else if (rangevalueX <'50') {
box.style.webkitTransform = "translateX(-" + (sliderx.value) + 'px)';
rangevalueX.value = sliderx.value;
}
};
Working fiddle http://jsfiddle.net/cn6St/2/

Categories