Help with If/Else Statement

Hello,

This seems very basic, but for some reason I can’t get it to work. I want the Onclick for button 60 to be called only if the if statement is valid (input10 has text).

I have tried many ways, and I simply can’t get it to work. However, the onclick event happens without the if statement.

($w(’ # input10’).value.length >= 8)
($w(’ #input 10’).value !== “”)
($w(’ #input 10’).value === true)

here is the code block that doesn’t seem to work :


 
if 
        ($w('#input10').value.length >= 8) 
{
  $w('#button60').onClick((event) => {

 let user = wixUsers.currentUser;
 let isLoggedIn = user.loggedIn;
 if (!isLoggedIn) {
      wixUsers.promptLogin().then(() => {
       processPlan(planId, planPrice);
     })
  } else {
      processPlan(planId, planPrice);
    }
 }
 );

}

The code for the entire page:

 
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
import wixPay from 'wix-pay';
import wixPaidPlans from 'wix-paid-plans';
import wixUsers from 'wix-users';

$w.onReady(function () {

 const currentPlanObject = $w("#dynamicDataset").getCurrentItem();
 const planId = currentPlanObject._id;
 const planPrice = currentPlanObject.price;

if 
        ($w('#input10').value.length >= 8) 
{
  $w('#button60').onClick((event) => {

 let user = wixUsers.currentUser;
 let isLoggedIn = user.loggedIn;
 if (!isLoggedIn) {
      wixUsers.promptLogin().then(() => {
       processPlan(planId, planPrice);
     })
  } else {
      processPlan(planId, planPrice);
    }
 }
 );

}
 

});

function processPlan(myId) {

 
    wixPaidPlans.purchasePlan(myId).then(orderObject => {
      wixWindow.openLightbox("Contact1", orderObject)
       .then((goForIt) => {
 if (goForIt) {
         wixPay.startPayment(orderObject.wixPayOrderId);
       }
      });
    })
 
}

Please help! I hope it’s just some small mistake I made.

Thanks in advance!

The logic is wrong.
According to your code, it goes like that:
Once the page is ready, it checks if input10 contains a long string and if that’s true it’s supposed to do X.
The problem is that this condition is always false. Because it checks the condition on page ready, before anyone has any chance to write any thing in the input box.

To fix that you need to use .onChange() handler.

–Also you should put a condition inside the .onChange() handler.

It works! Thank you so much for the help. You just saved me many more hours of debugging.

You’re welcome :slight_smile:

  • Make sure you made the condition right, so if a user writes an input and then deletes it, it won’t run the action.

How to write two conditions in if statement. my code is like
let minsize = 3000000 ;
let maxsize = 10000000 ;
let files = $w( “#uploadButton1” ).value;
let fileSize = files[ 0 ].size;
if (fileSize >= minsize && fileSize < maxsize){
console.log( “Valid Size” );
}

else {
console.log( ‘Invalid Size’ )
}
It’s not worked for me, please help.

And please let me know how to throw an error in else condition and how to display it.
Please help.
Thanks.

how would this code work if I have multiple inputs say 10 and I only want it to execute if the multiple inputs add up to less than 100 lets say.