Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exact Match help

    Everything works well except I can't make it tell the difference between "202" or "20"

    $("#recordInput").on("keypress", function (enter2) {
    if (enter2.keyCode == 13) {
    var record = $('input[name="record"]').val();
    var x = $("#drinks li:contains('" + record + "')");
    if (x.length){
    x.appendTo("#hugs");
    $('form').trigger("reset");
    }
    else{
    alert('Not Found');
    $('form').trigger("reset");
    }
    }
    });

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Then don't use :contains and just do an .each() loop through all the <li>'s in $("#drinks li') and compare manually.

    Btw, please enclose the code in [code] [/code] tags for readability.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Or you can add a data-record attribute containing the record value and then use the attribute selector.

    Code:
    <li data-record="20">20</li>
    <li data-record="202">202</li>
    Code:
    var x = $('#drinks li[data-record="' + record + '"]');
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Or use id if the record numbers don't repeat.

    Code:
    <li id="record_20">20</li>
    <li id="record_202">202</li>
    Code:
    var x = $('#record_' + record);
    Use class instead if the record numbers repeat.
    Last edited by glenngv; 09-24-2013 at 04:10 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Then don't use :contains and just do an .each() loop through all the <li>'s in $("#drinks li') and compare manually.

    Btw, please enclose the code in [code] [/code] tags for readability.

    Here is what I have now but something is off.

    Code:
    $('#recordInput').on('keypress', function(e) {
          if (e.keyCode === 13) { 
                var record = $('input[name="record"]').val(); 
                var x = $("#drinks li").each(function () {
        if ($(this).text() == record) {
            x.appendTo("#hugs");
            $('form').trigger("reset");
        } else {
          $('form').trigger("reset");
        }
    });
                if (x.length) {
                      x.appendTo('#hugs');
                           $('form').trigger("reset");
                }
                else {
                      
                      $('form').trigger("reset");
                }
          }
    });

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You can actually use .filter() instead of .each() but why don't you try my other solutions that don't require .filter() or .each()? Using record numbers in the id of li is the most efficient solution.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •