This question already has answers here:
Javascript string/integer comparisons
(9 answers)
Closed 6 years ago.
Anyone knows why showToast always pops up even if desiredquantity is greater than inventoryCount?
function checkavailable(){
var desiredquantity = document.getElementById("bin-Qty").value;
var inventoryCount = document.getElementById("bin-binItem-quantity").value;
var itemName = document.getElementById("bin-binItem").value;
if(desiredquantity > inventoryCount)
{
console.log(desiredquantity);
showToast('There are only ' +inventoryCount +' '+itemName+' left');
}
else
{
addToTransfer();
}
}
I have a button to that calls checkavailable
<button type="button"
onclick ="checkavailable();">
</button>
Please convert it to number first
Like this
if (+desiredquantity > +inventoryCount) { // + will parse it as number
console.log(desiredquantity);
showToast('There are only ' + inventoryCount + ' ' + itemName + ' left');
} else {
addToTransfer();
}
Or you can use parseInt if value is integer and if value float than use parseFloat
Like this
if (parseInt(desiredquantity) > parseInt(inventoryCount)) {
console.log(desiredquantity);
showToast('There are only ' + inventoryCount + ' ' + itemName + ' left');
} else {
addToTransfer();
}
Related
This question already has answers here:
Is there an easy way to convert jquery code to javascript? [closed]
(5 answers)
Closed 4 months ago.
Is it possible to rewrite the following code into pure javascript? I don't want to use jquery.
$(function () {
var aggregateInput = function () {
var value = ('Website Url: ') + jQuery('input[name="url"]').val() + ' \n \n' +
('Subject: ') + jQuery('input[name="subject"] \n').val() + ' \n \n' +
('Brief Message: ') + jQuery('textarea[name="customer service"] \n \n').val().replace(/\n/g, '<br/>');
jQuery('textarea[name="email-message"]').val(value);
}
jQuery('.contact-style').on('keyup', aggregateInput);
});
$(document).ready(function () {
$('.contact-form-button-submit').attr('disabled', true);
$('.customform-message').keyup(function () {
if ($(this).val().length != 0) {
$('.contact-form-button-submit').attr('disabled', false);
} else {
$('.contact-form-button-submit').attr('disabled', true);
}
})
});
document.addEventListener('DOMContentLoaded', function () {
var aggregateInput = function () {
var value = ('Website Url: ') + document.querySelector('input[name="url"]').value + ' \n \n' +
('Subject: ') + document.querySelector('input[name="subject"] \n').value + ' \n \n' +
('Brief Message: ') + document.querySelector('textarea[name="customer service"] \n \n').value.replace(/\n/g, '<br/>');
document.querySelector('textarea[name="email-message"]').value = value;
}
document.querySelector('.contact-style').addEventListener('keyup', aggregateInput);
});
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('.contact-form-button-submit').setAttribute('disabled', true);
document.querySelector('.customform-message').addEventListener('keyup', function () {
if (this.value.length != 0) {
document.querySelector('.contact-form-button-submit').setAttribute('disabled', false);
} else {
document.querySelector('.contact-form-button-submit').setAttribute('disabled', true);
}
});
});
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 5 years ago.
Even though I can clearly see my output in console. I can't write them into a file. There is 1 output file and its undefined-03-02-2017.txt which contains single line '15:33 undefined'.
for (i = 0; i < channelcount; i++) {
messages[i] -= messagesOld[i];
console.log(channels[i] + ' ' + messages[i]);
messages[i] = messagesOld[i];
fs.open('logs/' + channels[i] + '.txt', 'r', function (err, fd) {
if (err && err.code == 'ENOENT') {
fs.writeFile('logs/' + channels[i] + '-' + moment().format('MM-DD-YYYY') + '.txt', moment().format('H:mm') + ' ' + messages[i], function (err) { });
} else {
fs.appendFile('logs/' + channels[i] + '-' + moment().format('MM-DD-YYYY') + '.txt', moment().format('H:mm') + ' ' + messages[i] + '\n', function () { });
}
});
}
fs.open() is an async function. So your entire loop will run before the first callback actually gets called. By that time i equals channelCOount + 1 and channels[ channelCount + 1 ] is undefined.
You can either wrap the callback into a closure, use a let if you can use ES6, or use fs.openSync()
#Pyro, you used writeFile incorrectly. Please see syntax
writeFile(file, data[, options], callback)
you have to use like this
fs.writeFile('logs/' + channels[i] + '-' + moment().format('MM-DD-YYYY') + moment().format('H:mm') + '.txt ' , messages[i], function (err) { });
I have a label which displays the number of characters used in a textbox. When the page first loads it looks fine as it is small and in italics. I am using Bootstrap for that styling.
<label id="lblCharactersRemaining"><small><em>0 Out Of 255 Characters</em></small></label>
My JavaScript checks and updates the text in this label with the number of characters a user enters, this also works fine.
The problem is that each time the JS activates the styling goes back to the default MVC type. How can I make sure that when the JS executes that it doesn't remove the Bootstrap styling?
My JS is:
<script type="text/javascript">
var maximumLength = '#System.Configuration.ConfigurationManager.AppSettings["MaximumTextBoxLength"]';
if (document.getElementById("ObjectTextBox")) {
var txtbox = document.getElementById("ObjectTextBox");
txtbox.onkeyup = function () { IsMaxLength(this); };
}
function IsMaxLength(objTxtCtrl) {
if (objTxtCtrl.getAttribute && objTxtCtrl.value.length > maximumLength) {
objTxtCtrl.value = objTxtCtrl.value.substring(0, maximumLength);
}
if (document.all) {
document.getElementById('lblCharactersRemaining').innerText = (objTxtCtrl.value.length) + ' Out Of ' + maximumLength + ' Characters';
}
else {
document.getElementById('lblCharactersRemaining').textContent = (objTxtCtrl.value.length) + ' Out Of ' + maximumLength + ' Characters';
}
if (objTxtCtrl.value.length == 0) {
document.getElementById('lblCharactersRemaining').textContent = '0 Out Of ' + maximumLength;
}
}
</script>
Try :
function IsMaxLength(objTxtCtrl) {
if (objTxtCtrl.getAttribute && objTxtCtrl.value.length > maximumLength) {
objTxtCtrl.value = objTxtCtrl.value.substring(0, maximumLength);
}
if (document.all) {
$('#lblCharactersRemaining').html('<small><em>' + objTxtCtrl.value.length + ' Out Of ' + maximumLength + ' Characters</em></small>');
}
else {
$('#lblCharactersRemaining').html('<small><em>' + objTxtCtrl.value.length + ' Out Of ' + maximumLength + ' Characters</em></small>');
}
if (objTxtCtrl.value.length == 0) {
$('#lblCharactersRemaining').html('<small><em>0 Out Of ' + maximumLength + '</em></small>');
}
}
Every time you launch the function, it deleted the tags 'small' and 'em', so just add them...
UPDATE : PS : Using Jquery language...
UPDATE AFTER COMMENT :
BOOTPLY : http://bootply.com/106485
CODE :
JS :
$('button.jquery').on('click', function(){
$('#lblCharactersRemaining').html('<small><em>0 Out Of 85200</em></small>');
});
$('button.nojquery').on('click', function(){
document.getElementById('lblCharactersRemaining').innerHTML = '<small><em>0 Out Of 666</em></small>';
});
HTML :
<label id="lblCharactersRemaining"><small><em>0 Out Of 255 Characters</em></small></label>
<button class="jquery">Change label with jquery</button>
<button class="nojquery">Change label without jquery syntax</button>
I'm trying to create a tool that allows users to generate CSS code. At the moment I'm working on input - when a user leaves a text input empty, I'd like a message to appear. The code I have at the moment doesn't seem to work in doing so, any ideas?
$("#btn-css").click(function() {
if( $('input').attr(value) = "" ) {
$('#output').append('All boxes must be completed');
} else {
$('.preview').attr('style', 'box-shadow: ' + $("#h-value").val() + 'px ' + $("#v-value").val() + 'px ' + $("#blur").val() + 'px ' + $("#spread").val() + 'px #' + $("#colour").val() + ';');
$('#output').append('box-shadow: ' + $("#h-value").val() + 'px ' + $("#v-value").val() + 'px ' + $("#blur").val() + 'px ' + $("#spread").val() + 'px #' + $("#colour").val() + ';');
}
});
How about this it will select all text inputs on the page then filter them to be the ones that are empty.
$("#btn-css").click(function() {
var emptyInputs = $('input:text').filter(function() { return this.value == ""; });
if (emptyInputs.length === 0) {
// Everything filled in
} else {
$('#output').append('All boxes must be completed');
}
});
You might also want to do:
var emptyInputs = $('input:text').filter(function() { return $.trim(this.value) == ""; });
This will also pick up text inputs with just whitespace.
use .val().. val() does the exact same thing you mentioned..
if( $('input').val() == "" ) {
to check for all input..
var inputVallength=$('input').filter(function(){
return this.value==""
}).length;
if(inputVallength == 0 || inputVallength < $('input').length){
$('#output').append('All boxes must be completed');
}
$('input').attr(value)
This selects ALL input elements on the entire page.
And also what #Mike Christensen said, you need at least 2 equal signs.
Just do
$(':input').each(function(){
if(!$(this).val()){
//logic
}
});
a Quick fiddle
I am trying to insert around 58000 rows of a query inside a string. But after the row around 8000 I get a timeout error.
I've already tried to use SetTimeout funcions but it was of no use.
Check the code that I am working on:
function onQuerySuccess(tx, results) {
console.log("Entering onQuerySuccess");
if(results.rows) {
console.log("Rows: " + results.rows.length);
var len = results.rows.length;
if(len > 0) {
store_html(results, 0);
console.log("Finished Reading Rows: " + len);
saveNotes();
console.log("Finished Saving Notes");
} else {
//This should never happen
console.log("No rows.");
}
} else {
alert("No records match selection criteria.");
}
console.log("Leaving openView");
function store_html(results, rows_complete){
rows_complete=store_html_input(results, rows_complete);
console.log("Returning row:" + rows_complete);
if (rows_complete<results.rows.length)
{
setTimeout(store_html(results, rows_complete), 50);
}
}
function store_html_input(results, rows_complete){
for(var i = rows_complete; i < rows_complete+100; i++) {
gpsTextFile = gpsTextFile + results.rows.item(i).section + ' ' + results.rows.item(i).timestamp + ' ' + results.rows.item(i).latitude + ' ' +
results.rows.item(i).longitude + ' ' + results.rows.item(i).acx + ' ' + results.rows.item(i).acy + ' ' +
results.rows.item(i).acz + ' ' + results.rows.item(i).speed;
gpsTextFile = gpsTextFile + "\n\r";
}
return i;
}
So.. I get that "Javascript execution exceeded timeout".
Thank you for any of your help!
Best Regards.
You need to change your setTimeout() to NOT execute the function immediately. Change from this:
setTimeout(store_html(results, rows_complete), 50);
to this:
setTimeout(function() {store_html(results, rows_complete)}, 50);
As you had it before, it was immediately executing store_html(results, rows_complete) and passing the return value from that to `setTimeout() which was not delaying anything. This is a common mistake (2nd one of these problems I've answered today).