-1

I need to check for an empty input on a click event and if empty then halt the script. What is happening is that the script is still posting to the php script and totally ignoring my code. I have used an if statement to check but obviously, it isn't correct. I may need an else?

I would be grateful if someone could point out my error. Thanks

<script type="text/javascript">

$(function () {

  $('#srcRslt').click(function (e) {
    e.preventDefault();
    if ($('#srcBoxRslt').length == 0) {        <--- This the problem area

      notif({
        type: "error",
        msg: "<b>ERROR:<br /><br />You must enter a box to destroy</b>",
        height: 99,
        multiline: true,
        position: "middle,center",
        fade: true,
        timeout: 3000

      });
      return false;
    }
    var value = $('#srcBoxRslt').val();
    //alert(value);
    var qString = 'sub=' + value;
    //alert(qString);

    $.post('bdSrchadmin.php', qString, processResponse);
  });

  function processResponse(data) {
    //$('#resultsGoHere').html(data);
    //$('#boxdest').empty();
    //$("#boxdest").append("<option value='" + data + "'>" + data + "</option>");
    // $('#srcBoxRslt').val(data);
    $("#submit").prop("disabled", false);
    $("#submit2").prop("disabled", false);
    $("#submit3").prop("disabled", false);
    $('#srcBoxRslt').val('');
    $('#srcBox').val('');

    notif({
      type: "error",
      msg: "<b>SUCCESS:<br /><br />You have destroyed boxes.</b>" + data,
      height: 99,
      multiline: true,
      position: "middle,center",
      fade: true,
      timeout: 3000

    });
    $("#boxdest").load("bdrefreshBox.php");
  };
  //$('#boxdest').append('<option value="'.data.'" selected="selected">data</option>');
  //$('#boxdest').append(data);
  //alert(data);
});

</script>
user1532468
  • 1,723
  • 8
  • 41
  • 80
  • `$('#srcBoxRslt').length` returns the number of elements selected by `#srcBoxRslt` (which is probably 1). Also, please use the search. – Felix Kling Apr 17 '17 at 14:51

1 Answers1

3
$('#srcBoxRslt').length == 0

should be

$('#srcBoxRslt').val().length == 0

Also, maybe trim whitespace out

Fred Johnson
  • 2,539
  • 3
  • 26
  • 52