0

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

2
  • Where does the string come from? What's adding the `` characters? Commented Dec 4, 2013 at 21:40
  • Maybe an Ajax request? Commented Dec 4, 2013 at 21:43

2 Answers 2

2

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.

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

3 Comments

for some reason when I did it, the <\/option> and \n, etc would be there.
As far as I know the html() function in jQuery will take care of the deserialisation of the string for you.
well the thing is console.log(data) gives '...' where for a regular string console.log('foo') gives foo. Don't know if that matters.
0

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.

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.