Reputation: 566
I have an echo from a PHP script which is this:
echo "<select class='form-control' id='backgroundcolor' onchange='uploadFile()'>";
foreach($backgroundcolors as $cc => $name) {
echo '<option value="' . $name . '">' . $cc . '</option>';
}
echo "</select>";
It returns the following:
<select class='form-control' id='backgroundcolor' onchange='uploadFile()'><option value="#CD5C5C">Indian Red</option><option value="#000000">Black</option></select>
I then use the following code to add it to a div:
_("backgroundcolor").innerText = event.target.responseText;
Where I have defined the following function _:
function _(el) {
return document.getElementById(el);
}
BUt instead of giving me a select option it just shows the html in plain text on the site. If I copy the code and paste it to my site manually it works.
Upvotes: 2
Views: 95
Reputation: 976
You can use _("backgroundcolor").innerHTML = event.target.responseText;
But if you are using jquery, you can also use $('#backgroundcolor').html(event.target.responseText);
without those function above (function _(el)
).
Upvotes: 0
Reputation: 2134
Use innerHTML
instead of innerText
:
_("backgroundcolor").innerHTML = event.target.responseText;
innerText
adds the content as plain text, while innerHTML
adds the content as it is without encoding html characters.
Upvotes: 3