Convert text into HTML elements using jQuery [duplicate] - javascript

This question already has answers here:
jquery not working to change inner html?
(4 answers)
Closed 5 years ago.
My html file looks like this:
<div id="myDiv" class="div-sample"></div>
I have a text file which has the following text:
<b>abracadabra</b>
Now after reading the file from an ajax request(via a php script which just reads the file), when I write the following jQuery code:
$('#myDiv').innerHTML = text;
inspite of showing abracadabra it showing with the tags.
what is going on here?

The first line var text = '<b>abracadabra</b>' sets the variable text to be equal to the string with the open and closing tags.
The second line sets the contents of the $('#myDiv') to be equal to that variable '<b>abracadabra</b>'. Because they are valid html tags they get parsed and this is why you don't see them in the browser.
When you log the content of the variable text you get it's value not parsed by the browser with the tags which is '<b>abracadabra</b>'

Related

How can i make html code sample within a HTML? [duplicate]

This question already has answers here:
How to display raw HTML code on an HTML page
(30 answers)
Closed 1 year ago.
Suppose i need to write a website that gives a tutorial on html or codes.
<html><h1>Hello World</h1></html>
Then how can i write this code within it. i tried to use <code></code> and even <pre></pre> to enclose the html code so that it will not be intrepreted but it got intrepreted by the browser. So how can i display the html code within the html file in browser like a code sample..
You have to write htlm reserved code with html entities
https://www.w3schools.com/HTML/html_entities.asp
The 2 main ones
< -> <
> -> >

Is it possible to get the root HTML tag, not just body node? [duplicate]

This question already has answers here:
How to get the <html> tag HTML with JavaScript / jQuery?
(6 answers)
Closed 6 years ago.
In both of the following examples, speechTextString ends up including text from the entire document, including text outside of the BODY tag.
Example 1:
r = document.body;
speechTextString = r.innerText;
Example 2:
r = document.getElementsByTagName("BODY").item(0);
speechTextString = r.innerText;
Is it possible to get the root HTML tag, not just the body element?
text outside of the BODY tag
Assuming you aren't talking about the content of the <head> element:
You can't have text outside the <body> element. HTML does not allow it. The browser performs error recovery and moves it inside the <body> element when it parsing the (invalid) HTML into a DOM.
If you want to process a document with that error in it, then you will need to fetch the raw source code (e.g. with XMLHttpRequest) and then write a custom parser for it.

jQuery text() ignores leading blank [duplicate]

This question already has answers here:
Multiple Spaces Between Words in HTML without
(5 answers)
Closed 6 years ago.
I'm working on a script, that should fill a table with some string data.
Using jQuery's append and text function works fine for data without a leading blank symbol.
But if the string starts with one or more blank symbols they are ignored.
Please have a look at this code example: https://jsfiddle.net/e56eb0zx/2/
var blank = " blank ";
var no_blank = "no_blank";
$("tbody").append(
$("<tr>")
.append($("<td>").text("|"+blank+"|"))
.append($("<td>").text(blank)) // why isn't the blank displayed?
);
$("tbody").append(
$("<tr>")
.append($("<td>").text("|"+no_blank+"|"))
.append($("<td>").text(no_blank))
);
How can i fix this behaviour?
Thanks in advance!
jQuery is not the problem. The generated markup contains the blank before and after the word blank. Its a problem with rendering the table.
Set the CSS-Attribute white-space to pre or pre-wrap for your table cells to get the desired behaviour.
See this edited fiddle.

How to use a text file's contents in HTML as text [duplicate]

This question already has answers here:
Load .txt file using JQuery or Ajax
(4 answers)
Closed 8 years ago.
I am making a website template and I though of a cool new trick to save time when changing large text files. I want to have a separate plain text document that when the page loads, it contents will be transferred to my paragraph.
Example:
The text document:
Introducing the new red glasses.
The paragraph's text will be loaded and be the same as the text documents like so:
<p> Introducing the new red glasses. </p>
So how can I do something like this? Is it even possible? I am willing to use HTML, CSS, jQuery and Javascript.
You could use AJAX:
$.get( "mytext.txt", function( txt ) {
$( "#myParagraph" ).text( txt );
});
You will need to host your HTML from a server, or otherwise it won't work

How to get JavaScript script tags inside jQuery .html() function? [duplicate]

This question already has answers here:
Can I create script tag by jQuery?
(5 answers)
Closed 9 years ago.
Is it possible to get JavaScript script tags inside jQuery .html() function?
function pleaseWork(){
$('#content').html('<h3 style="color:#335c91">This is the header</h3><div class="holder"><script type="text/javascript" src="javascriptFile.js"></script></div>');
}
What I am trying to accomplish is when the user clicks on a button, this header and JavaScript code show up in the div content, right now when I click on the button, there is no header showing and it goes to a blank page and shows the JavaScript code (it works, but not how I would like it to work.)
Escape the </script> tag, by replacing it with this: <\/script>
If that doesn't work, also surround the script with cdata tags.
Working demo: http://jsfiddle.net/BgxZN/1/
EDIT:
If you want to actually show the contents of the javascript file inside the div, you'll need to use an XMLHTTP request or an iframe.
New working demo: http://jsfiddle.net/BgxZN/13/

Categories

Resources