0

So,I want to create a program,which can show me something,if I enter something (with the help of a datalist).

Because I don´t want to code for every possibility,I want to ask,if you can say,that the value of the Textbox should show the text inside of the variable with the name of the textbox.

One example :

There it would be nice,to be able to say : the variable with the name of document.getElementById("boxP").value should be shown(in this case : success)

<input type="text" name="srch" id="boxP" list="datalist1""></input>



<datalist id="datalist1">


<option value="TEST">

<option value="TEST1">


</datalist>

<script>
var TEST = "sucessful"

</script>

I don´t think,that you can do that(/ mark some output as a variable name),but it's worth a try,I think.

I´m happy about every answer!

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
Adolf Weii
  • 23
  • 6

2 Answers2

0

Try using a map inside of an eventListener:

document.getElementById('datalist1').addEventListener('input', function () {
   document.getElementById('boxP').value = textMap[this.value];
});

var textMap = {
  'TEST': 'successful',
  'TEST1': 'successful2'
};
Ryan C
  • 572
  • 5
  • 18
0

You can use an object, Define all the possibilities, Then index on it using the selected value from the Datalist.

var Obj = {
 'TEST': 'successful',
 'TEST1': 'successful1'
};
document.getElementById('boxP').addEventListener('change', function (e) {
   this.value = Obj[e.srcElement.value] || "";
});
<input type="text" name="srch" id="boxP" list="datalist1">



<datalist id="datalist1">
<option value="TEST">
<option value="TEST1">
</datalist>
Rainbow
  • 6,772
  • 3
  • 11
  • 28