locked
I think I have a loop $(this) issue RRS feed

  • Question

  • User1034446946 posted

    Hi

    I in my web page I have a number of hidden fields to identify if something has been printed, these are all start with 1 of 2 strings for the id so I have this:

        var checker = true;
        //Check Packing Slips
        $("input[id^='OnePrintReference_']").each(function (i, e) {       
                if ($(this).val() === false) {
                    checker = false;
                };
        });
    
        $("input[id^='TwoPrintReference_']").each(function (i, e) {
                if ($(this).val() === false) {
                    checker = false;
                };
        });
    
    //if check == false do nothing, else do something

    However the problem is it is always returning true, even when I have used the debugger and alerts to establish that there are false values, i should I check to see if there is something to loop through as sometimes there won't be a TwoPrintReference_,and whats the best way to do this

    Wednesday, September 12, 2018 2:26 PM

Answers

  • User475983607 posted

    A hidden field value type is a string not a Boolean.  A string is never strictly equal to false.

    The following link explains the === operator.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

    How are you setting the hidden fields and what is the value?  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 2:48 PM
  • User-474980206 posted

    as .val() returns a string:

                $(this).val() === false
    

    is always false. maybe you wanted 

                if ($(this).val() === "false") {
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 3:50 PM
  • User-893317190 posted

    Hi EnenDaveyBoy,

    As  pointed out by mgebhard and bruce (sqlwork.com), your check will always return false. 

    If you want to check whether the value of your input is "".You could write you code as follows.

     

    if ($(this).val() == false) {
                    checker = false;
                };

    Or directly 

     if ($(this).val()) {
                    checker = false;
                };

    For more information  falsy value, please refer to https://developer.mozilla.org/en-US/docs/Glossary/Falsy

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 13, 2018 3:18 AM

All replies

  • User475983607 posted

    A hidden field value type is a string not a Boolean.  A string is never strictly equal to false.

    The following link explains the === operator.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

    How are you setting the hidden fields and what is the value?  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 2:48 PM
  • User-474980206 posted

    as .val() returns a string:

                $(this).val() === false
    

    is always false. maybe you wanted 

                if ($(this).val() === "false") {
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 3:50 PM
  • User-893317190 posted

    Hi EnenDaveyBoy,

    As  pointed out by mgebhard and bruce (sqlwork.com), your check will always return false. 

    If you want to check whether the value of your input is "".You could write you code as follows.

     

    if ($(this).val() == false) {
                    checker = false;
                };

    Or directly 

     if ($(this).val()) {
                    checker = false;
                };

    For more information  falsy value, please refer to https://developer.mozilla.org/en-US/docs/Glossary/Falsy

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 13, 2018 3:18 AM
  • User1034446946 posted

    thanks for the replies.

    Thursday, September 13, 2018 11:55 AM