Converting escaped text to html in javascript?

I used $.get and received data like

'<option value=\"US-ID\" >Idaho<\/option>\n  <option value=\"US-IL\" >Illinois<\/option>\n  <option value=\"US-IN\" >Indiana<\/option>\n  <option value=\"US-KS\" >Kansas<\/option>\n'

I want to use $(...).html(html_data) how do I make the data I get into html data that I can use?

what I'm getting when $(...).html(data)

http://jsfiddle.net/9WeUv/

Don't know if this matters, but console.log(data):

'...data...'

whereas console.log('regular_string'):

regular_string // no quotes

Answers


What's wrong with:

var html_data = '<option value=\"US-ID\" >Idaho<\/option>\n  <option value=\"US-IL\" >Illinois<\/option>\n  <option value=\"US-IN\" >Indiana<\/option>\n  <option value=\"US-KS\" >Kansas<\/option>\n';

$('#select_element_id').html(html_data);

http://jsfiddle.net/45CYX/

After your edit:

Sorry, isn't the string you've received the one you wrote on top? In your jsFiddle you do not have any JS code, just some text in a select tag - which is not what you are saying in the question.


Assuming you grabbed the html from an ajax call and want to add it to the body.

$newSelect = $("<select></select>").html(html_data);
$(document.body).append($newSelect);

http://api.jquery.com/jQuery/#jQuery2 - Creates DOM elements on the fly from the provided string of raw HTML.


Need Your Help

Google Cocoapods Not Installing

ios objective-c cocoapods google-analytics-sdk

Whenever I try to install any of Google's cocoapods I get one of two errors.

Calculate answer from list

java list calculator rpn

I have to make a calculator in java that is able to work with brackets, plus, minus, divide and multiply, so far I've got it so if the user were to enter: