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>;
You need to add some quotes and concatenation to that
function getvaluefromScenariotwo(name){
return $('input[data-scenario="' + name + '"]').val();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With