suppose i have value 'v' in some variable say 'var', now i want to display Valvo(text respect to value v). any suggestion??
Asked
Active
Viewed 120 times
0
-
There couldn't be a variable with name `var` :) – VisioN Mar 13 '13 at 09:10
-
Do you mean this: http://stackoverflow.com/questions/5613834/convert-string-to-variable-name-in-javascript ? – Hvordan har du det Mar 13 '13 at 09:14
-
@VisioN that was just an example... – user2046302 Mar 13 '13 at 09:31
-
Can you be a little more specific? Are you trying to use String.substring()? – HeremansY Mar 13 '13 at 09:11
2 Answers
0
HTML
<select id="sel">
<option value="Volvo">Volvo</option>
<option value="BMW">BMW</option>
</select>
JS
var myvar = 'b';
var elem = document.querySelector('#sel option[value^='+myvar.toUpperCase()+']');
if (typeof elem != 'undefined') {
elem.selected = true;
}
Here you can test -> http://jsbin.com/ehayav/1
mz

mariozski
- 1,134
- 1
- 7
- 20
0
May be this will help you :
//array of varables
var names = ['apple','banana','valvo','blahblahblah']
//this is your variable
var variable = 'v'
//code to display names starting with the character in your variable
for(i=0;i<names.length;i++)
if(names[i][0] == variable)
alert(names[i])
This answer is for my interpretation of your question. EnjoY!

codeVerine
- 742
- 3
- 14
-
Please make your question more clear. What you want to exactly do ? Do you want to dynamically create a select box like this : '' – codeVerine Mar 13 '13 at 10:20