uwem
uwem

Reputation: 65

How to insert JSON.parse value into DOM

How can i append a JSON value gotten from an ajax request to an element? Below is the value of the result after i parsed it to JSON.

{
"result": "success",
"action": "",
"message": "<option value='1'>30 days</option><option value='2'>60 days</option>"
}

This is the element

<select>
    <span id="o_p"></span>
</select>

I have tried $('#o_p').append(res.message) where res is the value gotten after JSON.parse(AJAX_result) but the value of select remains empty

Upvotes: 0

Views: 208

Answers (1)

Quentin
Quentin

Reputation: 943214

Your HTML is invalid.

  • A span cannot be a child element of a select.
  • An option cannot be a child element of a span.

Remove the span. Append the message to the select.

const AJAX_result = `{
"result": "success",
"action": "",
"message": "<option value='1'>30 days</option><option value='2'>60 days</option>"
}`;

const res = JSON.parse(AJAX_result);

$('select').append(res.message)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select></select>

Upvotes: 1

Related Questions