How do I convert a javascript string to HTML to be displayed in a form?
ok I know there has to be a way to do this, but I can't find out how anywhere. I am a newb to jQuery and JavaScript, so I am sorry if this is a dumb question. I am calling a cfc (coldfusion) from jQuery and returning HTML. The problem is, when I retu开发者_StackOverflowrn the data, it is in the form of a JavaScript string and is being displayed as plain text on the page, instead of being processed as the HTML. Is there a function, or a way to convert the string back to HTML?
This would be how to write html inside of a jQuery element:
$('#id_of_element_to_put_html_inside').html(your_javascript_string);
Perhaps you're looking for element.innerHTML
?
document.write(data);
would be a straight up javascript way of putting the code straight on the page if you're only writing whatever is returned from the cfc. But, as the comment below mentioned, this would need to be the only thing present on your page.
Let's assume though, that you have a page like this:
<html>
<head></head>
<body>
<div><p id="dynamicContent"></p></div>
</body>
</html>
Your jQuery would then take the data returned by the cfc and add it to the DOM like so:
$('p#dynamicContent').html(data);
(edited in light of the document.write comment)
Thank you all for your answers. I figured out the issue. This is the function I was using to display my HTML return grom my cfc:
function displayForm(data){
//alert(data);
$(".displayCountryForm").html(data);
}
As it turns out, it was an issue with the CFC. I needed to include the "returnFormat" attribute in the cffunction as "plain". Once this was done, I was able to get my return to render on the page. Thank you all again for your answers.
精彩评论