locked
jquery shows   to have length of 1, how can I detect   in jquery? RRS feed

  • Question

  • User1868852945 posted

    In jquery I have logic to determine if an element has any text by checking its length...

    if ($("#myelement").text().length > 0)

    When the element has   the length is 1 so I need a way to detect for  

    How can I check for   ?

    I only care about the top browsers - chrome, firefox and IE 9 or above.

    Thanks

    Friday, October 14, 2016 12:55 PM

Answers

  • User1868852945 posted

    This worked for all three browsers.

    if ($("#myelement").length > 0 && $(yellow_from_text).text() != '\xa0')

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2016 9:00 PM

All replies

  • User-654786183 posted

    Instead of

    if ($("#myelement").text().length > 0)

    Try the following code

    if($.trim($("#myelement").val()).length > 0)
    {
         // Your code here
    }
    

    This will trim both the leading and trailing whitespaces.  You can also refer this fiddle

    Friday, October 14, 2016 1:23 PM
  • User1868852945 posted

    This worked for all three browsers.

    if ($("#myelement").length > 0 && $(yellow_from_text).text() != '\xa0')

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2016 9:00 PM