4

How can i get the input value using data-attributes with jQuery? This is my function which is not working. the return value of valuefromscenariotwo should be "My test 2"

var valuefromscenariotwo = getvaluefromScenariotwo("scenario2");
function getvaluefromScenariotwo(name){
  return $(input[data-scenario=name]).val();
}

<div class="form-group">
<input type="text" data-scenario="scenario1" class="form-control notes" placeholder="Enter Notes"           value="My test 1">
</div>;
<div class="form-group">
<input type="text" data-scenario="scenario2" class="form-control notes" placeholder="Enter Notes"   value="My test 2">
</div>;
<div class="form-group">
<input type="text" data-scenario="scenario3" class="form-control notes" placeholder="Enter Notes"   value="My test 3">
</div>;

1 Answer 1

8

You need to add some quotes and concatenation to that

function getvaluefromScenariotwo(name){
    return $('input[data-scenario="' + name + '"]').val();
}
Sign up to request clarification or add additional context in comments.

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.