I have the folowing code:
var price1 = (length1 * width1) * priceSM / 10000;
if (price1 < 10) {
alert("Size too small");
return;
}
document.getElementById('result1').innerHTML = Math.round(price1);
What I need to do:
If price 1 < 10 alert AND then price1 = 10, but if price1 = 0, or bigger than 10, that's fine.
length1 and width1 are INPUTS
Please help, I'm at very beginning with javascript.
I don't know if I understand well what you want.
Try this ?
var price1=(length1*width1)*priceSM/10000;
if(price1 < 10 && price1 != 0){
alert("Size too small");
price1 = 10;
return;
}
document.getElementById('result1').innerHTML=Math.round(price1);
If you want to update your element "result1" with price1 when < 10 then just remove "return;"
If it isn't what you want, please tell us and post all function's code please.
To avoid the alert when the price is zero, add that in the condition with the && operator:
price1 > 0 && price1 < 10
To set the price to 10 in that case, add this to the if block:
price1 = 10
To really show the modified price (10), you should remove the return statement from that if block. That way the code will continue to execute the last statement.
Finally, not really a problem, but it is better to use the textContent property instead of the innerHTML property when you intend to assign something that is plain text and not HTML.
So your code then becomes:
var price1 = (length1 * width1) * priceSM / 10000;
if (price1 > 0 && price1 < 10) {
price1 = 10
alert("Size too small");
}
document.getElementById('result1').textContent = Math.round(price1);
Using .value property you are able to set the value.
document.getElementById("myText").value = "Hello World...";
Related
I'm creating the following web app for my employer:
https://jsfiddle.net/kupe2rLL/3/
How it Works: The Missed Opportunity and Engage Results fields populate dynamically as users enter information in the Company Info section.
The Problem: I need to program the app so that the Years in Business input takes no value less than 1 and no value greater than three when calculating the jobsRecovered variable and RevenueYear variable only.
<input type="text" id="yearsOpen" name="yearsOpen" class="form-control" onchange="calculateAll()" required>
var jobsRecovered = (averageJobs * 50) * recovery * yearsOpen;
var revenueYear = jobsRecovered * jobValue;
Preferably, I need the Years in Business input field to accept any value from 0 to infinity on the form input but then change the value of that variable to a minimum of 1 and maximum of 3 when making calculations for the jobsRecovered variable and RevenueYear only. I'd like to implement this solution using a JavaScript function if possible but I am open to alternate solutions.
I researched and implemented the following solution but unfortunately this only limits the min - max range of input on the form itself:
input.addEventListener('change', function(e) {
var num = parseInt(this.value, 10),
min = 1,
max = 3;
if (isNaN(num)) {
this.value = "";
return;
}
this.value = Math.max(num, min);
this.value = Math.min(num, max);
});
Any assistance would be appreciated. (Note: My experience with programming is Beginner level.)
To limit the range of the variable value, you can pass it through if-else statements, or a ternary operation which act like a filter for your values.
e.g.
if (yearsOpen <= 3) ? (if (yearsOpen < 1) ? (yearsOpen = 1) : (yearsOpen = yearsOpen)) : (yearsOpen = 3);
smaller statement if (n <= 3) ? (if (n < 1) ? (n = 1) : (n = n)) : (n = 3);
If-else format:
if (yearsOpen <= 3) {
if (yearsOpen < 1) {
yearsOpen = 1;
} else {
yearsOpen = yearsOpen;
}
} else {
yearsOpen = 3;
}
Hope that makes sense.
I am working on getting the user t input a number for something in this case it would be a resistor value, I have asked them via a prompt to pick a number and then if that number is within the limits I have set then the number would be displayed back to the user within a div.
I have built this already and got it to work with a couple of test messages so I believe the function itself is fine however I am having a problem that whenever the user enters a correct value that value isn't displayed but "undefined" is displayed instead.
This is the HTML I am testing,
<button onclick="R1Value()">Change Value</button>
<div id="test3"></div>
And this is the JavaScript function
function R1Value() {
var R1ValueEntered = prompt("please enter a value for R1:")
var R1 = parseInt(R1ValueEntered)
var display = document.getElementById('test3');
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1.value;
}
else {
alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}
}
I have placed into into a jsfiddle as even though there isn't a lot of code it may save you some time if you can have a look, http://jsfiddle.net/2ufnK/72/ I may be missing something simple but I can't seem to fix the problem.
Just remove .value :
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1;
}
FIDDLE
.value is undefined for integers. Remove that and your code will work fine.
You don't need the R1.value. Just calling R1 will return it's value.
you got an error here, you assign R1 value, not R1.value
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1;
}
I tried your code.
Besides that you might have missed a closing tag, it worked for me. You need to change these lines:
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1.value;
} else {
alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}
to:
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1;
} else {
alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}
you don#t need to get the value of R1, because it already IS hte Value.
I hope i could help.
regards
Your problem is that you are never closing your <div> and you also call R1.value, which is basically calling .value on an integer, which is undefined. Try the following:
function R1Value() {
var R1ValueEntered = prompt("please enter a value for R1:")
var R1 = parseInt(R1ValueEntered)
var display = document.getElementById('test3');
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1;
} else {
alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}
}
<button onclick="R1Value()">Change Value</button>
<div id="test3"></div>
So here's my code:
var score1 = $(this).attr('data-score1');
var score2 = $(this).attr('data-score2');
if (score1 < score2) {
// do some
}
if (score2 > score1) {
// do something else
}
Now, this works fine as long as both variables are either both < or both > 100, yet whenever either of those variable is larger than 100 while the other is not the wrong if statement gets triggered. What the hell could be going on here?
Thanks for any advice!
Use parseInt()
The attributes will throw up strings.. So when you try to compare them... You are actually comparing
"100" > "90" and not 100 > 90 .. Using parseInt() with a radix should solve your problem..
var score1 = parseInt( $(this).attr('data-score1') , 10);
var score2 = parseInt( $(this).attr('data-score2') , 10);
if (score1 < score2) {
// do some
}
else if (score2 > score1) {
// do something else
}
As #naveen suggested you could do this as well
var score1 = +$(this).attr('data-score1');
var score2 = +$(this).attr('data-score2');
You're comparing the values as strings. The string "90" begins with 9, which has an ascii code that is greater than that of 1.
You can convert it to a number by using parseInt
parseInt(score1, 10)
so, my problem here is that my code seems to be recognizing that 100 is < 2000, but its not recognizing that 200 < 1000
heres my code (i also use jquery as a framework FYI)
$('.filter-price').submit(function(e) {
var alert_message = '';
var price_from = $('.filter-price #price_from').val();
var price_to = $('.filter-price #price_to').val();
if (isNaN(price_from))
{
alert_message += "Price from must be a number, i.e. 500\n";
$('.filter-price #price_from').val('From');
}
if (isNaN(price_to))
{
alert_message += "Price to must be a number, i.e. 500\n";
$('.filter-price #price_to').val('To');
}
if (!isNaN(price_from) && !isNaN(price_to) && (price_from >= price_to))
{
alert_message += "Price from must be less than price to\n";
$('.filter-price #price_from').val('From');
$('.filter-price #price_to').val('To');
}
if (alert_message != '')
{
e.preventDefault();
alert(alert_message);
}
});
i've tried using parseInt() on the vars which fixes nothing.
Sorry, but you really need to do this way:
var price_from = parseInt($('.filter-price #price_from').val(), 10);
var price_to = parseInt($('.filter-price #price_to').val(), 10);
Look the result on chrome console:
'200' >= '1000'
true
200 >= 1000
false
And if you don't want to limit the numbers to int, replace parseInt(val, 10) to parseFloat(val)
parseInt works for me. Not sure what was incorrect for you.
http://jsfiddle.net/98Bzn/1
var price_from = parseInt($('.filter-price #price_from').val());
var price_to = parseInt($('.filter-price #price_to').val());
Where have you tried using parseInt()? It seems to me that it's interpreting your values as strings rather than numbers, so you need to coerce them into the correct data type.
I would do this:
function convertCurrencyToNumber(value) {
return Number(value.replace(/[^0-9\.]+/g,""));
}
...
var price_from = convertCurrencyToNumber($('.filter-price #price_from').val());
var price_to = convertCurrencyToNumber($('.filter-price #price_to').val());
It appears you're using currency, so the above will convert to a decimal for database storage or whatever else you're doing.
I want to assign value to the GridView Column Name TaxAmount. To get the TaxAmount I am doing some calculation, Even if the Calculation retruns -ve value, I want to assign +ve value to the Column.
For ex, if the Calculation returns 5.003 then it should be 5.003
even if returns -5.003 then it should be 5.003.
How to assign, I have given my JavaScript below.
var taxgrid = document.getElementById('<%=gvAttribute.ClientID %>');
var rwIndex = 1;
var taxamount = 0*1;
for (rwIndex; rwIndex <= gvRowCount - 1; rwIndex++)
{
for(i = 0; i < taxip.length; i+= 4)
{
var sign = taxip[i+2].value;
var tax = taxip[i+3].value;
taxamount = parseFloat(taxamount) + (sign == '+' ? 1 : -1) * parseFloat(tax);
taxgrid.rows[rwIndex].cells[5].getElementsByTagName('input')[0].value = taxamount;
}
}
I used TemplateFields in the GridView.
Have you tried Math.abs()
To know more about Math.abs() check w3schools
Try like followng
taxgrid.rows[rwIndex].cells[5].getElementsByTagName('input')[0].value =Math.abs(taxamount);