0

I store values in a multi-dimensional hidden input array that looks like this:

<input type="hidden" name="tokens[0][Search_Type]" value="a" />
<input type="hidden" name="tokens[0][Search_Term]" value="123" />
<input type="hidden" name="tokens[1][Search_Type]" value="b" />
<input type="hidden" name="tokens[1][Search_Term]" value="456" />

How can I quickly check whether there is a token with Search_Term = X and Search_Type = Y? If there's a way to do it in one jquery line rather than in a loop that'd be awesome.

3 Answers 3

2

Jquery:

token_found = $('input[name$=Search_Type]][value=Y]
                 +
                 input[name$=Search_Term]][value=X]'
               ).length > 0;
Sign up to request clarification or add additional context in comments.

Comments

2

You could do it with selectors as well:

// found
console.log($('input[name$="[Search_Type]"][value="a"]').next('input[name$="[Search_Term]"][value="123"]').length); 

// not found
console.log($('input[name$="[Search_Type]"][value="b"]').next('input[name$="[Search_Term]"][value="123"]').length); 

// found
console.log($('input[name$="[Search_Type]"][value="b"]').next('input[name$="[Search_Term]"][value="456"]').length); 

Example: http://jsfiddle.net/niklasvh/56jLf/

But whether or not that is more efficient than with looping, I don't know.

Comments

0

If you are checking the submitted array as a javascript object, you can use jsonpath to perform xpath like queries on a dataset.

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.