Reputation: 187
I have this very simple calculator which I have written in JavaScript. Basically it works out the profit between two integers with the tax amount being 5%.
I would like the output (#result
) to change colour depending if the result number is either a positive or negative integer.
Here is my calculator's HTML:
Buying Price
<br /><br />
<input id="buying" type="text">
<br /><br />
Selling Price
<br /><br />
<input id="selling" type="text">
<br /><br />
<input type="submit" onclick="output();">
<p id="result" name="r1"></p>
Here is my calculator's JS:
function output(){
var buying = document.getElementById('buying').value;
var selling = document.getElementById('selling').value;
parseInt(selling) / 20;
document.getElementById('result').innerHTML = parseInt(selling) - parseInt(buying) - parseInt(selling) / 20;
}
Here is a JS fiddle: http://jsfiddle.net/ac81ee78/
I have attempted using jQuery for this however it did not work.
Please if anyone could help me with this it would be greatly appreciated.
Upvotes: 5
Views: 3920
Reputation: 8216
jQuery
example (takes decimals as well)
$(document).ready(function() {
$('.submit').on('click', function() {
var buying = parseFloat($('#buying').val());
var selling = parseFloat($('#selling').val());
var result = (selling - buying - (selling/20)).toFixed(2);
$('#result').html(result);
if (result < 0) {
$('#result').css('color', 'red');
}
else {
$('#result').css('color', 'green');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Buying Price
<br /><br />
<input id="buying" type="text"/>
<br /><br />
Selling Price
<br /><br />
<input id="selling" type="text"/>
<br /><br />
<input class="submit" type="submit" />
<p id="result" name="r1"></p>
Upvotes: 3
Reputation: 20399
I modified your code a bit so it changes color based on the sign of the output. jQuery isn't needed for this - we can use plain JS functions to change the color of the output box.
Live Demo:
var resultEl = document.getElementById('result');
function output() {
var buying = document.getElementById('buying').value;
var selling = document.getElementById('selling').value;
var resultVal = parseInt(selling) - parseInt(buying) - parseInt(selling) / 20;
resultEl.innerHTML = resultVal
if (resultVal >= 0) {
resultEl.style.color = "green";
} else {
resultEl.style.color = "red";
}
}
Buying Price
<br />
<br />
<input id="buying" type="text">
<br />
<br />Selling Price
<br />
<br />
<input id="selling" type="text">
<br />
<br />
<input type="submit" onclick="output();">
<p id="result" name="r1"></p>
JSFiddle Version: http://jsfiddle.net/ac81ee78/2/
Upvotes: 5
Reputation: 115282
You can do something like this based on the result value with help of ternary operator
function output() {
var buying = document.getElementById('buying').value,
selling = document.getElementById('selling').value,
res = document.getElementById('result'),
result = parseInt(selling) - parseInt(buying) - parseInt(selling) / 20;;
res.innerHTML = result;
res.style.color = result >= 0 ? 'green' : 'red';
}
Buying Price
<br />
<br />
<input id="buying" type="text">
<br />
<br />Selling Price
<br />
<br />
<input id="selling" type="text">
<br />
<br />
<input type="submit" onclick="output();">
<p id="result" name="r1"></p>
Upvotes: 3