0

I have several input elements, that only differ on their data- attribute.

<input name="country" data-action="buy"/>
<input name="country" data-action="sell"/>

I'm trying to get the value that the user entered into the input with a "buy" data-

What's the selector I should use?

$("input[name=country] ????").val()

(There is a reason they have the same name and no id that's not related to the question)

3 Answers 3

2

Just add data-action="buy" also as an attribute selector

$('input[name="country"][data-action="buy"]').val()
Sign up to request clarification or add additional context in comments.

Comments

1
$("input[data-action='buy']").val()

Comments

1
$("input[name='country']").filter(i) {
  return $(this).data('action') == 'buy';
}).val();

2 Comments

This is an interesting answer, thanks. What's the advantage over a plain selector?
I don't know that there is an advantage but it provides a little practice with the .filter() method.

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.