Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get input value using data-attribute with jquery

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>;
like image 332
Carlitos Overflow Avatar asked Feb 11 '23 17:02

Carlitos Overflow


1 Answers

You need to add some quotes and concatenation to that

function getvaluefromScenariotwo(name){
    return $('input[data-scenario="' + name + '"]').val();
}
like image 115
adeneo Avatar answered Feb 14 '23 23:02

adeneo