Checking the field for a value using jquery

I am adapting a jQuery codebase (its a spinning prize wheel) and decided to add a form input for email, I have tied the .submit to the “spin” .click function, everything works, except I need
the wheel to not spin if the input field is empty. Could someone please look at the code and see where I am going wrong?

$('#spin').one('click', function(){

    if $('#login').val() == ''
//   exit so the wheel doesn’t spin.

                  $(".profile-form").submit();
        
                 //add 1 every click
                 clicks ++;
                 
                 /*multiply the degree by number of clicks
          generate random number between 1 - 360, 
    then add to the new degree*/
                 var newDegree = degree*clicks;
                 var extraDegree = Math.floor(Math.random() * 360) + 1;
                 totalDegree = newDegree+extraDegree;
                 
    var colorIndex = Math.ceil(((totalDegree+30) % 360) / 60) -1;
    var colorPrev = colorIndex == 0 ? 5 : colorIndex - 1;
    var colorNext = colorIndex == 5 ? 0 : colorIndex + 1;
    
    offset = (extraDegree % 60);
    var result = sec[colorIndex];
    
    if(offset == 0) {
      result;
    } else if (offset <= 30) {
      result;
    } else {
      result;
    }
    
    $("#answer").html("Congratulations! You've received a " + result);

You can just return from the function, and it shouldn’t spin.

if ($("#login").val() === "") {
    return;
}

This type of conditional check at the start of some code is typically called a guard clause, for it protects the rest of the code from being executed until the conditions are right for that to occur.

1 Like

Thank You soo much, I have been working on this for days!!
your solution works great! I tried every but a conditional check

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.