-2

I think I'm missing something basic here. Why is not IF condition true? Shouldn't the condition evaluate to false?

how all condition gets true.

function subdetails(){
    var name = $("#name").val();
    var email = $("#emailid").val();
    var state = $("#state").val();
    var city = $("#city").val();
    var titile = $("#ctitle").val();
    var cname = $("#cname").val();
    var cdesc = $("#cdesc").val();
    var disamt = $("#disamt").val();

    if(name && email && state && city && title && cname && cdesc && disamt !== '') {
      alert();
    }

HTML:

<button type="button" onclick="return subdetails();" data-toggle="modal" data-target="#myModal" id="step3top" class="btn btn-submit">Next</button></div>  
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 2
    well, what are the values of all those variables? are they all "truthy" and `disamt !== ''`? – Bravo Oct 17 '19 at 07:53
  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output. – mplungjan Oct 17 '19 at 07:53
  • Please provide a better explanation and a context of all the variables used. – sibasishm Oct 17 '19 at 07:54
  • I think this is not exactly doing what you THINK it is doing - it is translating to `if ( (name is truthy - i.e. name !="" && name !== null && name != undefined && name !=0) && same for the others && disamt !== '')` – mplungjan Oct 17 '19 at 07:56
  • What are the values of all those elements? – VLAZ Oct 17 '19 at 07:56
  • `Why is not IF condition true?` so, you asking why is it false ... `Shouldn't the condition evaluate to false?` so you're saying it should be false - – Bravo Oct 17 '19 at 07:56
  • these all should not be null – Praveen Pachauri Oct 17 '19 at 07:57
  • Not only `null` values are falsy - [check the JS equality table](https://dorey.github.io/JavaScript-Equality-Table/) – VLAZ Oct 17 '19 at 07:58
  • 1
    Variable defined is named `titile` while the function is comparing with `title`, which can be not defined in that case. Is that the error or just a typo in the code ? – jmtalarn Oct 17 '19 at 07:59
  • Also just add `required` to each field and make the button a submit button – mplungjan Oct 17 '19 at 08:01
  • @jmtalarn thanks . i was not expected like this typo error . – Praveen Pachauri Oct 17 '19 at 08:19
  • Here is a more elegant version https://jsfiddle.net/mplungjan/p34fkzv7/ – mplungjan Oct 17 '19 at 08:30

1 Answers1

0

I am assuming you think that

if(name && disamt !== '')

equals

if(name !== '' && disamt !== '')

however, this is not true, you can't shorten it like that. You need to write the condition for every variable. Your given code

if(name && email && state && city && title && cname && cdesc && disamt !== '') {
  alert();
}

equals to this when written out:

if(name == true && email == true && state == true && city == true && title == true && cname == true && cdesc == true && disamt !== '') {
  alert();
}

What you need to write is this:

if(name !== '' && email !== '' && state !== '' && city !== '' && title !== '' && cname !== '' && cdesc !== '' && disamt !== '') {
  alert();
}
MiK
  • 918
  • 4
  • 16
  • 1
    `if(name ...)` is not equal to `if(name === true ...)` when written out, consider `name="abc"`, only the first if will tirgger because name is truthy, not equal to `true` – Nick Parsons Oct 17 '19 at 08:00
  • Corrected to == – MiK Oct 17 '19 at 08:02
  • That still isn't equal to `if(name ...)`: https://jsfiddle.net/ztfqey54/ I think it would be more like `if(Boolean(name) === true ...)`, or just `if(Boolean(name) ...) ` – Nick Parsons Oct 17 '19 at 08:11
  • `equals to this when written out:` no, `if (name)` doesn't transform into `if (name === true)` nor even to `if (name == true)` - it's `if (name !== false && name !== undefined && name !== 0 && name !== "" && name !== NaN)` – VLAZ Oct 17 '19 at 08:20
  • I have already used these codes. but now solved the problem. it was typo error. titile=>title . – Praveen Pachauri Oct 17 '19 at 08:23