7

I have the need to store JSON in a HTML Select object, and wondering if it's possible?

For example:

<select id="music" >
  <option value={mbid:123,artist:'Judas Priest'};>Heavy Metal 1</option>
  <option value="{mbid:456,artist:'Black Sabbath'};">Heavy Metal 2</option>
  <option value="{mbid:789,artist:'Iron Maiden'};">Heavy Metal 3</option>
  <option value="{mbid:102,artist:'Accept'};">Heavy Metal 4</option>
</select>

$('#music').on('change', function() {
  var val = this.value;
  alert(val.mbid)
});

JSFiddle example: https://jsfiddle.net/rv7x38cw/

3 Answers 3

8

Can you please modify your HTML and JS code as follows:

https://jsfiddle.net/rv7x38cw/2/

HTML:

<select id="music">
  <option value='{"mbid":123,"artist":"Judas Priest"}'>Heavy Metal 1</option>
  <option value='{"mbid":456,"artist":"Black Sabbath"}'>Heavy Metal 2</option>
  <option value='{"mbid":789,"artist":"Iron Maiden"}'>Heavy Metal 3</option>
  <option value='{"mbid":102,"artist":"Accept"}'>Heavy Metal 4</option>
</select>

JS:

$('#music').on('change', function() {
  var str = this.value;
  alert(str);
  var val = $.parseJSON(str);
  alert(val);
  alert(val.mbid)
});

Here, the key changes are; you have to put value=' your JSON string ' in single quote and each JSON's key name has to be surrounded by double quote like "mbid"

You can also checkout in below live fiddle.

    $('#music').on('change', function () {
        var str = this.value;
        alert(str);
        var val = $.parseJSON(str);
        alert(val);
        alert(val.mbid)
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="music">
    <option value='{"mbid":123,"artist":"Judas Priest"}'>Heavy Metal 1</option>
    <option value='{"mbid":456,"artist":"Black Sabbath"}'>Heavy Metal 2</option>
    <option value='{"mbid":789,"artist":"Iron Maiden"}'>Heavy Metal 3</option>
    <option value='{"mbid":102,"artist":"Accept"}'>Heavy Metal 4</option>
</select>

Sign up to request clarification or add additional context in comments.

Comments

3

As described in HTMLOptionElement: value DOMString Reflects the value of the value HTML attribute, if it exists; otherwise reflects value of the Node.textContent property.

And:

DOMString is a UTF-16 String. As JavaScript already uses such strings, DOMString is mapped directly to a String.

So, yes, you can use a json value, but this value must be serialized as string:

  • JSON.parse() parses a JSON string, constructing the JavaScript value or object described by the string.
  • JSON.stringify() converts a JavaScript object or value to a JSON string

$(function () {
  $('#music').on('change', function() {
    var val = JSON.parse(this.value);
    console.log(val.mbid);
  });
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

<select id="music" >
    <option value='{"mbid":123,"artist":"Judas Priest"}'>Heavy Metal 1</option>
    <option value='{"mbid":456,"artist":"Black Sabbath"}'>Heavy Metal 2</option>
    <option value='{"mbid":789,"artist":"Iron Maiden"}'>Heavy Metal 3</option>
    <option value='{"mbid":102,"artist":"Accept"}'>Heavy Metal 4</option>
</select>

1 Comment

"JSON.stringify() converts a json object to string" is wrong. Firstly, there is no such thing as a "json object" because JSON is a string. Secondly, JSON.stringify() converts an object to a JSON string. Also, "JSON.parse() convert a string to an object" reads wrong too. JSON.parse() converts JSON to an object, not a string.
1

One of ways to do it is put the value as a string for example:{mbid:456,artist:'Black Sabbath'} and access the elements using $.parseJSON(val) or JSON.parse(val):

$('#music').on('change', function() {
   var val = this.value;
   val = JSON.parse(val);
   alert(val.mbid);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.