1

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
  • compare 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 compare one string against another. Does anybody know of a better way to approach this?

1
  • Remove your second "var" keyword on exampleAddressPresent. Because it redefines it locally, and you won't be able to use it globally. Commented Apr 25, 2013 at 10:52

2 Answers 2

4

You don't need match() to compare two strings. There is the common === compare 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
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Yep saw it ! And remove the var cause this var is already defined globally !
index is an optional parameter of the .each() method holding the index of the current element inside the jQuery object
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.