Title: Objects in JavaScript using jQuery
Tags: javascript;jquery
Question: It does not show me data from the object..
<div class="form-data"&gt;
<select name="option"&gt;
<option value="0"&gt;</option&gt;
<option value="1"&gt;Name</option&gt;
<option value="2"&gt;Lastname</option&gt;
<option value="3"&gt;Age</option&gt;
</select&gt;
</div&gt;
<div id="output"&gt;</div&gt;
This is SELECT in HTML
var obj = {
name: "Alex",
lastname: "Strukov",
age: "21"
$("select[name='option']").on("change", function() {
var value = $("select[name='option']").val();
switch (value) {
case 1:
$("div#output").text(obj["name"]);
break;
case 2:
$("#output").text(obj["lastname"]);
break;
case 3:
$("#output").text(obj["age"]);
break;
}
});
For example: I want to select "Name", and let the object display the data
Comment: The code looks OK! Something must probably be wrong in the HTML then

Comment: The `value` of an `input` or `select` is always a string. `switch` uses `===`-style comparisons, so your cases will never match, as they're numbers, not strings. Either make them strings, or parse the value.

Comment: Side note: In your handler, no need to re-query the DOM, just use `var value = $(this).val();`

Comment: Thanks to everyone, now everything works!

Comment: `val()` returns a string, so it needs to be `case "1":` etc

Comment: Hi! Show us your HTML code from your `` to help us ;)

Comment: What are the values in the select? Can you create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)

Here is the accepted answer: Add onchange handler to the select &amp; Pass the value of the selected function.
In your case the object does not have a key by name 1,2,3. So $("select[name='option']").val(); will not able to retrieve any value by key from the object
var obj = {
name: "Alex",
lastname: "Strukov",
age: "21"
function showValue(val) {
$("#output").text(obj[val]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;</script&gt;
<select onchange="showValue(this.value)"&gt;
<option value ="name"&gt;Name</option&gt;
<option value ="lastname"&gt;lastname</option&gt;
<option value ="age"&gt;age</option&gt;
</select&gt;
<div id="output"&gt;</div&gt;
Comment for this answer: Works! Thank you


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:04/09/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><jquery>', 'Title': 'Objects in JavaScript using jQuery', 'ViewCount': 59, 'id': 49735941, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}