jquery - Javascript match string against string in array -
i have set of input fields, each class "smarty_address_check"
<input type="text" class="smarty_address_check" value="this new value" /> <input type="text" class="smarty_address_check" value="this value has been unchanged" /> etc
what need
- for each input field value
- compare value each of values in array (array called smarty_address_check)
- if matches, something.
the values in array original default/example values of input fields, , if user hasn't changed them want act on that.
var exampleaddresspresent = false; (var = 0; i<smarty_address_check.length; i++) { $('.smarty_address_check').each(function() { //for each of inputs if (smarty_address_check[i].match($(this).val())) { //if match against array var exampleaddresspresent = true; //example address present console.log($(this).attr("id")); //store id of unchanged input } }); }
i feeling bad programming logic, not mention can't work out why isn't working properly. want compare 1 string against another. know of better way approach this?
you don't need match()
compare 2 strings. there common ===
compare operator
if(smarty_address_check[i] === $(this).val()) {
edit: if index of array matches index/position of input, can avoid outer loop using same index
$('.smarty_address_check').each(function(index) { //for each of inputs if (smarty_address_check[index] === $(this).val()) { //if match against array exampleaddresspresent = true; //example address present console.log($(this).attr("id")); //store id of unchanged input } });