0

On a page with 4 possible input values arriving from a previous page (page can be gotten to as a step 2 of a multistep form), im attempting to write jquery so that: IF field values arrive empty, OR no fields are there at all,do nothing, else trigger a function. no problem with the else and function part but I don't know how to do the first part: write or define in Jquery "empty" (""?) or "non-existant" (undefined?), and not sure how to tell it "do nothing" ({}?).

here is my best attempt:

$(document).ready(function(){

var val1Get = $("#val1Pass").val();
var val2Get = $("#val2Pass").val();
var val3Get = $("#val3Pass").val();
var val4Get = $("#val4Pass").val();

if(val1Get + val2Get + val3Get + val4Get) == (' "" || undefined ') {}
else { 
$("#wrapper").prepend('<div id="mssg11"><div id="bnnr"><h3>Welcome</h3></div></div>');
});
PhilD
  • 277
  • 1
  • 5
  • 16

2 Answers2

2

Why don't you look at it from the different perspective... And ONLY do action if the values are NOT empty, that way you don't need else .. and half of your question falls off the board. As for checking for value being null or empty, see this jQuery: checking if the value of a field is null (empty)

It's up to you if you want to wrap it into function or not.

Community
  • 1
  • 1
olexity
  • 105
  • 4
1

You could append all the values together and then check the length. No need to make an empty if code block when you can just check if the length is greater than 0 then prepend your div.

$(document).ready(function(){
    var val1Get = $("#val1Pass").val();
    var val2Get = $("#val2Pass").val();
    var val3Get = $("#val3Pass").val();
    var val4Get = $("#val4Pass").val();

    if((val1Get.trim() + val2Get.trim() + val3Get.trim() + val4Get.trim()).length > 0)
        $("#wrapper").prepend('<div id="mssg11"><div id="bnnr"><h3>Welcome</h3></div></div>');
});
DSlagle
  • 1,563
  • 12
  • 19