1

Can anyone tell me what's wrong and how to fix this bit of code...

I am trying to bring up a message and not go to the next page, if the #send_country box has either nothing in it or says "Send From..." (as its the placeholder).

This is the bit of code I am having issues with:

if (country == '0' || country == "Send From...") {
    error = 1;
    jQuery('.msg').text("Please Select A Country.");
}

I think I have an issue with the OR function as it works without the || country == "Send From...".

<script>
    jQuery(document).ready(function(){
        jQuery('.submit').click(function() {

            var error = 0;
            var country = jQuery('#send_country').val();
            var countrys = jQuery('#delv_country').val();
            var idsa = jQuery('.size').val();

            if (country == '0' || country == "Send From...") {
                error = 1;
                jQuery('.msg').text("Select A Country.");
            }
            if (countrys == '0') {
                error = 1;
                jQuery('.msg').text("Select A Country.");
            }

            if (error) {
                return false;
            } else {
                return true;
            }

        });

    });
</script>

1 Answer 1

1

if the #send_country box has either nothing in it or says "Send From..." (as its the placeholder).

The placeholder attribute is different than the value, you couldn't get the placeholder using .val(); instead you should use .prop('placeholder') :

var country_placeholder = jQuery('#send_country').prop('placeholder');

So the condition will be :

if (country == '0' || country_placeholder == "Send From...") {

But like this the condition will always return true since the placeholder will not change if you're filling the field, so i suggest just to check if the value is empty or '0' :

if (country == '0' || country == "") {

Hope this helps.

jQuery('.submit').click(function() {
  var error = 0;
  var country = jQuery('#send_country option:selected').val();

  if (country == '') {
    error = 1;
    jQuery('.msg').text("Select A Country.");
  }

  if (error) {
    console.log('EROOR');
  } else {
    console.log('SUBMIT');
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <select type="text" id='send_country'>
    <option value="" disabled selected>Send From...</option>
    <option value="1">1</option>
    <option value="2">2</option>
  </select>

  <button class='submit'>Submit</button>
</form>

<span class='msg'></span>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I have made this my first option <option value="" disabled selected>Send From...</option> as the placeholder as I am having trouble, can I just make it see the "Send From..." in the or statement?
I think i got you, the problem is in the way you're retrieving the value of the select, use var country = jQuery('#send_country option:selected').val();. (check my updated snippet).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.