javascript - Select my troublesome input by value using jQuery -


i have html inputs similar , go voucher1 - voucher10:

<input maxlength="20" type="text" name="voucher1" id="voucher1" title="voucher" class="input" value=""> 

there 10 fields , each 1 has unique name , id. have function combines field values array , returns variable known duplicate if voucher typed in previously. goal select input greatest id (i.e. alphanumerically: voucher10 greater voucher8) , null field containing duplicate while displaying "no duplicates allowed" message.

my duplicate variable function working , returns duplicate value using .on(input, function). have message displaying whenever type duplicate value. problem i'm having selecting 1 input matches duplicate value. if inspect inputs in browser, there appears no dom representation of typed value. type "ninja" in 3 or 4 fields , never show. because of that, seems can't select inputs based on value.

i rather not loop through each voucher , check individually. seems jquery inspect inputs contain "ninja" , use .val("") null out. tricky html attribute "value" never changes. ideas?

here code finds duplicate within array in case needs primary focus selecting html input value attribute never changes. below function works fine aside ".ninja" contains part failed attempt:

$(".voucher-input").focusout(function(){ var sorted_goodvouchers = vouchers.sort(); // can define comparing function here.                               // js default uses crappy string compare. var results = []; (var = 0; < vouchers.length - 1; i++) {     if (sorted_vouchers[i + 1] == sorted_vouchers[i]) {         results.push(sorted_vouchers[i]);         // if there duplicates         console.log("results");         console.log(results);         $(".ninja"):contains(results).val("fail");         $( "#dupmsg" ).show( "slow" );     } else {       $( "#dupmsg" ).hide( "slow" );     } } }); 

i confess don't understand current duplicate detection function. doesn't correspond understanding of description of trying achieve. in particular, don't believe there need sort anything. sorting costly , should avoided.

if understanding correct, duplicate-detection function build , return jquery collection containing fields duplicate values, ignoring of lowest index in set of duplicates.

$(function($) {     function findduplicates($fields) {         var duplicates = []; //empty array         $fields.each(function (i, field_i) {             $fields.each(function (j, field_j) {                 if (j > && field_i.value != '' && field_i.value === field_j.value) {                     duplicates.push(field_j);                 }             });         });         return $(duplicates); //return jquery collection of duplicate fields     }      var $voucherfields = $("input[name^='voucher']").on('focusout', function () {         var $duplicates = findduplicates($voucherfields.removeclass('duplicate')).addclass('duplicate');         if ($duplicates.length) {             $("#message").text("no duplicates allowed"); //put message in message field         }     }); }); 

demo

notes:

  1. the j > i test ensures :
    • each field not compared itself
    • only fields of higher index included in returned collection.
  2. the field_i.value != '' test ensures
    • empty fields not compared

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -