I have a set of input fields, each with the class "smarty_address_check"
<input type="text" class="smarty_address_check" value="this is a new value" />
<input type="text" class="smarty_address_check" value="this value has been unchanged" />
etc
What I need to do is
- for each input field value
- pare that value to each of the values in the array (array is called smarty_address_check)
- if it matches, do something.
The values in the array are the original default/example values of the input fields, and if the user hasn't changed them I want to act on that.
var exampleAddressPresent = false;
for (var i = 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 get the feeling this is bad programming logic, not to mention I can't work out why it isn't working properly. All I basically want to do is pare one string against another. Does anybody know of a better way to approach this?
I have a set of input fields, each with the class "smarty_address_check"
<input type="text" class="smarty_address_check" value="this is a new value" />
<input type="text" class="smarty_address_check" value="this value has been unchanged" />
etc
What I need to do is
- for each input field value
- pare that value to each of the values in the array (array is called smarty_address_check)
- if it matches, do something.
The values in the array are the original default/example values of the input fields, and if the user hasn't changed them I want to act on that.
var exampleAddressPresent = false;
for (var i = 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 get the feeling this is bad programming logic, not to mention I can't work out why it isn't working properly. All I basically want to do is pare one string against another. Does anybody know of a better way to approach this?
Share Improve this question asked Apr 25, 2013 at 10:27 BarneyBarney 1,8485 gold badges31 silver badges51 bronze badges 1- Remove your second "var" keyword on exampleAddressPresent. Because it redefines it locally, and you won't be able to use it globally. – Flo Schild Commented Apr 25, 2013 at 10:52
2 Answers
Reset to default 4You don't need match()
to pare two strings. There is the mon ===
pare operator
if(smarty_address_check[i] === $(this).val()) {
EDIT: If the index of the array matches the index/position of the input, you can avoid the outer loop by using the 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
}
});
If the array
you are checking against is an Array of strings
, a possible solution would be :
Convert the values in the Array to one single string:
var myArray = ['apples', 'mangoes', 'bananas'] // example Array of strings
var string = myArray.join(':'); // we'll get "apples:mangoes:bananas"
Next, we can check if our value is on that string
var myValue = 'apples' // example input value
if(string.indexOf(myValue) !== -1)
// do something (i.e. myValue matches with one of the values on myArray)
Good Luck