Wrong result after compare select.val() and span.text()

NechiK

I have a folowing problem.
On click button Filter I want get entries with selected city.
SO forbid me write big code, so I show only div with data. I think you can add select with options, if you want.
(Select id = "cityFilter" , options: Dnepr, Kharkiv, Lviv)

HTML:

<div id="data">
    <div>
        <a class="userEmail" href="#">[email protected] </a>
        <span> user1 </span>
        <span> Dnepr </span>
        <span><input type = "button" value = "x" /></span>
    </div>
    <div>
        <a class="userEmail" href="#">[email protected] </a>
        <span> user2 </span>
        <span> Kharkiv </span>
        <span><input type = "button" value = "x" /></span>
    </div>
    <div>
        <a class="userEmail" href="#"> [email protected] </a>
        <span> user3 </span>
        <span> Lviv </span>
        <span><input type = "button" value = "x" /></span>
    </div>
    <div>
        <a class="userEmail" href="#"> [email protected] </a>
        <span> user4 </span>
        <span> Dnepr </span>
        <span><input type = "button" value = "x" /></span>
    </div>
    <div>
        <a class="userEmail" href="#"> [email protected] </a>
        <span> user5 </span>
        <span> Lviv </span>
        <span><input type = "button" value = "x" /></span>
    </div>
</div>

cityFilter.js (The description of the code in comments):

$(function() {
  //click on button
  $('body > input').on('click', function() {
    //get selected value
    var cityFilter = $('#cityFilter').val();
    //sort out spans which contains city names
    $('#data > div > a + span + span').each(function() {
      //for check the received value
      console.log($(this).text());
      //compare selected value and span text
      if ($(this).text() != cityFilter) {
        console.log('true');
      }
    });
  });
});

I got all true. Where is the problem ?

Jaromanda X

your spans have leading and trailing spaces

i.e. " abc " != "abc"

try the following instead:

 if ($(this).text().trim() != cityFilter) {
    console.log('true');
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

After changing jpa entity at runtime,select gives wrong result

From Dev

select with IN gives wrong result

From Dev

Kotlin - wrong val result in class with secondary constructor

From Dev

Changing text after a span

From Dev

Select and replace a text within a span

From Dev

Select text inside <span> onClick

From Dev

Select text inside <span> onClick

From Dev

Select query showing wrong result

From Dev

compare dates which gives me wrong result

From Java

Compare result to the previous result where after grouping

From Dev

Regex to select between span content and then seperate result

From Dev

Dynatable js show modal after select val

From Dev

Remove text after <span> and append other text

From Dev

jQuery .val() for select element returns text

From Dev

jQuery .val() for select element returns text

From Dev

Select2 Templating <span>text</span> in selection render

From Dev

Vertically aligning span with text, select, and span with image on single line?

From Dev

<span> Text updates based on <select> option

From Dev

Not able to select a text under "span" using selenium

From Dev

something wrong with the result of mysql query with joins and select

From Dev

Laravel add select query returning wrong result

From Dev

MySQL SELECT query returns wrong result

From Dev

MYSQL 'select count(distinct col)' the result is wrong?

From Dev

How to add text into span after document.createElement("span");?

From Dev

How to get the text after a span using jquery (multiple span)

From Dev

How to encapsule text before and after <span> into another <span>?

From Dev

remove text from <p> AFTER <span>

From Dev

CSS How to hide text after a span

From Dev

Python:extract text after </span> before <br/>

Related Related

  1. 1

    After changing jpa entity at runtime,select gives wrong result

  2. 2

    select with IN gives wrong result

  3. 3

    Kotlin - wrong val result in class with secondary constructor

  4. 4

    Changing text after a span

  5. 5

    Select and replace a text within a span

  6. 6

    Select text inside <span> onClick

  7. 7

    Select text inside <span> onClick

  8. 8

    Select query showing wrong result

  9. 9

    compare dates which gives me wrong result

  10. 10

    Compare result to the previous result where after grouping

  11. 11

    Regex to select between span content and then seperate result

  12. 12

    Dynatable js show modal after select val

  13. 13

    Remove text after <span> and append other text

  14. 14

    jQuery .val() for select element returns text

  15. 15

    jQuery .val() for select element returns text

  16. 16

    Select2 Templating <span>text</span> in selection render

  17. 17

    Vertically aligning span with text, select, and span with image on single line?

  18. 18

    <span> Text updates based on <select> option

  19. 19

    Not able to select a text under "span" using selenium

  20. 20

    something wrong with the result of mysql query with joins and select

  21. 21

    Laravel add select query returning wrong result

  22. 22

    MySQL SELECT query returns wrong result

  23. 23

    MYSQL 'select count(distinct col)' the result is wrong?

  24. 24

    How to add text into span after document.createElement("span");?

  25. 25

    How to get the text after a span using jquery (multiple span)

  26. 26

    How to encapsule text before and after <span> into another <span>?

  27. 27

    remove text from <p> AFTER <span>

  28. 28

    CSS How to hide text after a span

  29. 29

    Python:extract text after </span> before <br/>

HotTag

Archive